Mike
Mike

Reputation: 379

How can I tell jQuery to recognize the ids inside the table?

I had a table in which the buttons click did not work based on the jQuery function I defined inside document.ready. Thanks to Stackoverflow, I tried the following code and the buttons now work on click (showing their id):

        $('.myspecialtable').on('click',"button.btn", function(){
            console.log(this.id);
        });

Inside that table, there also divs and spans which have different ids. Unfortunately, they're not recognized by jQuery. For example, I want to change the text inside a span with id="aggregated-score". Again using Stackoverflow previous answered questions, I try the following and nothing happens:

        $('.myspecialtable').on('click',"button.btn", function(){
            console.log(this.id);
            $('#aggregated-score span').text('changed!');
        });

Even when I try:

            var x = $('#aggregated-score span').text('changed!');
            console.log(x)

nothing comes up in the console which made me think it is not acting anything on the id. What am I doing wrong? How can I tell jQuery to recognize the ids inside the table.

Upvotes: 0

Views: 27

Answers (1)

t.niese
t.niese

Reputation: 40852

#aggregated-score span selects an element with the tag span that is an descendant of an element that has aggregated-score as id so so for this code:

<span id="aggregated-score"><span></span></span>

#aggregated-score span would select the inner span.

To select a span with the aggregated-score as id you have to use span#aggregated-score or just #aggregated-score as an id has to be unique anyways.

Upvotes: 1

Related Questions