Skip to content
Advertisement

How can I change the text inside my dynamically created button’s with jQuery?

I have reviewed How can I change the text inside my <span> with jQuery? to get where I am. However, that does not cover dynamically created buttons.

I am creating buttons dynamically which contain a label.

JavaScript

enter image description here

The three “this.patExeId” values are

  • OA==
  • Mg==
  • Mw==

I want to update the label as exercise sets are completed. I am using this test code that is triggered when the button is clicked:

JavaScript

This is triggering an error shown in the console log:

JavaScript

Advertisement

Answer

Since in your case you are crafting a selector like #id span but the element you are using to grab the id is this so you can just make it simpler and directly select an element inside that one using find()

JavaScript

But to better address your first concern, you were using a selector with an id containing special characters. The = has a special meaning in css so it requires to be escaped if you need it to include it literally. To make a very simple example showing that, if you had an element with id #0== you should use the following selector:

JavaScript

And this would be an alternate solution to your problem just for the sake of showing my point:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement