Hanfei Sun
Hanfei Sun

Reputation: 47051

How to change the element with no ID in jquery?

The HTML codes are like this:

<div id="select1_chzn" class="chzn-container chzn-container-single" style="width: 250px;">
     <a class="chzn-single chzn-single-with-drop"
      href="javascript:void(0)" tabindex="-1">
     <span>AFF5</span>
<div></div>
</div>

I was wondering how I could change the <span>AFF5</span> to <span>Another</span> in jquery. Does anyone have ideas about this? Thanks!

Upvotes: 6

Views: 1171

Answers (7)

John Dvorak
John Dvorak

Reputation: 27287

The title says "How to change the element with no ID in jquery?"

If you want to target all spans with no ID within the div #select1_chzn, you could do

$("#select1_chzn span:not([id])").text(...);

It seems like you want the last span:

$("#select1_chzn span:last").text(...);

Upvotes: 3

alemangui
alemangui

Reputation: 3655

parent child, like this:

$("#select1_chzn span").text('Another');

Upvotes: 3

Bruno
Bruno

Reputation: 5822

You can actually search for spans based on their contents if that is the only way you have of identifying them.

​$("span:contains(AFF5)").text( "Another" );

However, this is case sensitive.

Upvotes: 0

SAJ14SAJ
SAJ14SAJ

Reputation: 1708

Use the CSS selector

#select1_chzn span

I am not jquery person, but I have seen enough questions here on SO to guess that you would use

$('#select1_chzn span')

Upvotes: 7

AbstractChaos
AbstractChaos

Reputation: 4211

Could use the id of it parent

$("#select1_chzn span").text("Another");

UPDATE

Using > means direct descendant where as a space means descendant not necessarily direct.

Upvotes: 12

michaelward82
michaelward82

Reputation: 4736

$("#select1_chzn span").text("Another");

Simple use of a standard CSS descendant selector should do in this case.

Upvotes: 5

Maxim Pechenin
Maxim Pechenin

Reputation: 344

You can use any available css-selector for selecting jquery objects, for example this should work:

$('span').text('Another');

Upvotes: 0

Related Questions