3

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.

1
  • 3
    Not, it's read-only. Use jQuery's replaceWith instead. Commented Oct 30, 2012 at 10:06

2 Answers 2

1

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');
Sign up to request clarification or add additional context in comments.

6 Comments

I suppose OP wanted to change all occurrences, not just the color in the style.
@Srcee It depends on whether you append the markup or not, this doesn't change the property of your data object, can you provide a demo on jsfiddle.net?
Ok can you please see my edited question again. Your code changes the color, but it still doesn't work - the text in my textarea stays red.
@Srcee The color of text of textarea remains red because you cannot append an element to it, it only accepts text content.
So how can I convert element to text?
|
1

Use

$(data.description).replaceWith(function(){
    return this.outerHTML.replace('red', 'black')
});

Demonstration

If you want to change only first one, you may use this :

$(data.description).eq(0).replaceWith(function(){
    return this.outerHTML.replace('red', 'black')
});

3 Comments

@dystroy But you code changes the color of the whole data.description, I want it to change only the line with [i] index
And then do I need to call this replaceWith method somehow?
Ok can you please see my edited question again. Your code changes the color, but it still doesn't work - the text in my textarea stays red.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.