Alireza Farahani
Alireza Farahani

Reputation: 2539

Changing text after a <br> with jquery

I have a span tag like this and stored in the variable "foo"

<span class="right margin">
    <a href="./?p=filmdetail&amp;fid=644&amp;fileid=722&amp;t=میراث چینی 3D">سی ئی 1500</a>
    <br>
    Nicole Kidman
</span>

Using jQuery OR javascript, how can I change the "Nicole Kidman" clause with something I want? innerHTML changes everything in the <span>

Edit: I edited the text with this code foo[0].lastChild.nodeValue="something else"; but is there another way doing that using only jQuery?

Upvotes: 0

Views: 139

Answers (3)

Kabie
Kabie

Reputation: 10663

Here the foo is not a jQuery object:

foo.lastChild.textContent='something you want'

So the foo is a jQuery object, but you still need to access the dom element directly:

foo.contents().eq(-1).textContent='something you want'

Upvotes: 1

Gaurav Bhor
Gaurav Bhor

Reputation: 1157

  1. <span> is an inline element. We avoid using <br/> in them.
  2. It is also advisable to use another tag to encapsulate your 'Nicole Kidman' text.(Like another <span>)

I'm not aware of your entire HTML structure so I'm going with what you have posted. Here is a fiddle example for how it can be done.

Upvotes: 1

yakiang
yakiang

Reputation: 1634

I recommend you add another span tag around Nicole Kidman.

<span class="right margin">
    <a href="./?p=filmdetail&amp;fid=644&amp;fileid=722&amp;t=میراث چینی 3D">سی ئی 1500</a>
    <br>
    <span>Nicole Kidman</span>
</span>

and change the text like this:

$(".right").find("span").text("something you want");

Upvotes: 3

Related Questions