fishgold192
fishgold192

Reputation: 57

How to change value in childnode

Today I have a problem. I can't get and change value in array Childnotes. I try find in stackoverflow but not results as expected

var $this = $(this);

When Debug, I receive image follow:

enter image description here

I want to get value and change value in childNotes: NodeList[3] --> 1:img --> change attributes: src in img

Upvotes: 0

Views: 354

Answers (2)

Dimitar Dimitrov
Dimitar Dimitrov

Reputation: 15148

Maybe you can try something like:

$(this).children("img").attr("src", "mycool_image.jpg");

This will find the direct children -> if you have them nested deeper, you can use find() instead.

$(this).find("img").attr("src", "mycool_image.jpg");

EDIT

If you want the n-th child img element then you can do:

$(this).find("img").eq(2).attr("src", "mycool_image.jpg");

EDIT2

To get the source before changing it simply do (by supplying the correspondent index to eq() of course):

var source = $(this).find("img").eq(2).attr("src");

Upvotes: 1

Diego Polido Santana
Diego Polido Santana

Reputation: 1435

I think that you would like to change some attr src from image tag inside your tag

So, you can use your reference $this (tag ) to find your specific image tag and do whatever your want:

...

var $this = $(this);
$this.find("img").attr("src", "some_value");

...

If you have more than one image tags and you want to change just the third tag, you should mark it with some "data-attribute", like this:

$this.find("img[data-changeable]").attr("src", "some_value");

Upvotes: 1

Related Questions