0

I want to change my button's text value by finding that button by its id. i have one variable buttionid='3' and trying below code:

$("#slips :button[id=buttionid]").html('mytext'); // it is not working

however below code is working:

$("#slips :button[id='3']").html('mytext');

I need to find button with its id but id is provided by user so search could be dynamic.

Use case is:

I have one input tag form there i am fetching inputs like (3,10)

so I want to assign value as '10' to button with id='3'.

I hope my question is clear.

  • 2
    `$("#" + buttionid).html('mytext');` – Alon Eitan Apr 24 '20 at 13:11
  • Because an id value can only be used once on a page, you don't need any of the other contextual stuff in the selector string. Just `"#" + buttonid` is all you need. – Pointy Apr 24 '20 at 13:12
  • but that button is in one specific div which is slips. – JainKaushal Apr 24 '20 at 13:14
  • 1
    That does not matter. If you use the same id more than once, things probably won't work no matter what you do in your selector string. The old "Sizzle" library would ignore everything else in the selector once it determined that the target was an id selector, and I would not be surprised if the implementation of `querySelector()` in browsers did the same thing. – Pointy Apr 24 '20 at 13:14
  • ok but what if it is i want to select with class attribute which can be duplicate. – JainKaushal Apr 24 '20 at 13:16
  • @JainKaushal Update the question with the usecase you mean. – mplungjan Apr 24 '20 at 13:17
  • Selecting by class is quite different than selecting by id; your question explicitly said "id". – Pointy Apr 24 '20 at 13:22

1 Answers1

0
$(`#slips :button[id=${buttionid}]`).html('mytext');
mplungjan
  • 169,008
  • 28
  • 173
  • 236