YSFKBDY
YSFKBDY

Reputation: 886

jQuery change dynamically created element's text

I've got two spans with the same id. One is normal and the other one is dynamically created. Btw, I can not change ids, they must be the same.

<span id="mySpan">Hello, World 1!</span>
<span id="mySpan">Hello, World 1!</span>

I am doing an ajax request and after the result comes, i want to change dynamically created span's text. First one is changing, and i guess i can not reach to the second one.

<span id="mySpan">Hello, World 1!</span>
<span id="mySpan">Hello, World 2!</span>

I've been trying to do this for an hour, but no luck.

Upvotes: 0

Views: 54

Answers (2)

Himanshu Upadhyay
Himanshu Upadhyay

Reputation: 6565

You should not use same id multiple times in a DOM. DOM considers only first id from top if you have defined same id more than once.

This is the reason why it is working with first and not with others.

change id attribute to class and then deal with the class using jquery to change the text.

Upvotes: 2

Renzo Calla
Renzo Calla

Reputation: 7696

You can select both spans with the attribute property

$('[id="mySpan"]') //selects both elements 

but isn't a good practice to use the same id for multiple elements..

Upvotes: 1

Related Questions