Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change outerHTML in javascript or jquery

I want to change the outerHTML and this is what I do:

$(data.description)[0].outerHTML = $(data.description)[0].outerHTML.replace('red', 'black');

The value is:

$(data.description)[0].outerHTML: "<p style="color: red;">desc1</p>"

and it doesn't get changed. How to change it?

data.description has this value:

<p style="color: red;">desc1</p><p style="color: red;">desc2</p>

And I want only $(data.description)[0] changed.

Here is my whole code:

var ingsLines = $(data.description);
for (var i =0; i < ingsLines.length; i++) {
    if (someCondition) {
        $(data.description).eq(i).css('color', 'black');
    }
}
try{$('#myTextarea').html(data.description);}catch(err){}

And the code changes the value, it puts black instead of red, but data.description stays with red.

like image 269
petko_stankoski Avatar asked Dec 29 '25 22:12

petko_stankoski


1 Answers

If you want to change the color property of the first element, you can use css and eq methods:

$(data.description).eq(0).css('color', 'black');
like image 67
undefined Avatar answered Jan 01 '26 11:01

undefined



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!