How to target a button from a group of buttons in JQuery without a class name or id

How can I target a button from a group of buttons without using a class or id, just n JQuery


  <button>Click Me</button>
  <button>Click Me</button>
  <button class="food">Click Me</button>
  <button>Click Me</button>
  <button>Click Me</button>
  <input type="text">
     <script src="…>
     <script src="index.js"></script>

>Solution :

With JQuery this would only be possible if the button’s had unique innerText or innerHTML that you could then predict.

Using my example HTML from below:


You would need to use JQuery to grab all tags, and then filter that by their innerHTML or innerText depending on what is inside the button tag.

See the below JS:

// Assuming you want 'Button 1'
for(let el of $("button")){
  if(el.innerText === 'Button1') el.addEventListener() // Then from here do the rest

Leave a Reply