2

I have got a variable with a table:

var tableCode='<table><tr><td><span class'thisone'>'the_text_to_change'</span><span class'thisone'>'other_text_to_change'</span><span class'thisone'>'another_text_to_change'</span></td></tr></table>'

I would want to replace the text inside each span with the class 'thisone', each one depending on the value inside of the class.

So the variable tableCode should be:

var tableCode='<table><tr><td><span class'thisone'>'the_text_changed_1'</span><span class'thisone'>'the_text_changed_2'</span><span class'thisone'>'the_text_changed_3'</span></td></tr></table>'

I have been reading the documentation of each: https://api.jquery.com/each/, but it is for the DOM and not for a variable in Javascript.

So: Read the variable, go throw it and detect the spans with a specific css class and change the value depending on the text.

Thank you very much!!!

1
  • 2
    1) Your code is wrong (cfr the colors, if you use single quotes everywhere it can't work unless you escape it) 2) Try to actually write some code 3) Come back her if you're stuck Commented Apr 3, 2014 at 14:45

2 Answers 2

1

One way

Fiddle Demo

var tableCode = "<table><tr><td><span class='thisone'>'the_text_to_change'</span><span class='thisone'>'other_text_to_change'</span><span class='thisone'>'another_text_to_change'</span></td></tr></table>";
tableCode = $(tableCode); //make it a jQuery Object
tableCode.find('span.thisone').text(function (i) {
    return 'the_text_to_change_' + ++i;
}); //find span with class thisone and set text
console.log(tableCode.html()); //to retrieve the changed string use tableCode.html()
Sign up to request clarification or add additional context in comments.

13 Comments

This code is not replacing the text inside the spans
@Za7pi change your table structure too as in fiddle .
It is working, but one thing, if you put the variable like this(adding two tr): var tableCode = "<tr><td><span class='thisone'>the_text_to_change</span></td></tr><tr></td><span class='thisone'>other_text_to_change</span><span class='thisone'>another_text_to_change</span></td></tr>"; It is not working, it is changing only the first span. Thanks!!
@Za7pi you markup was wrong --> here </tr><tr></td><span you have invalid </td> tag change it to var tableCode = "<tr><td><span class='thisone'>the_text_to_change</span></td></tr><tr><td><span class='thisone'>other_text_to_change</span><span class='thisone'>another_text_to_change</span></td></tr>"; --> jsfiddle.net/cse_tushar/Wzn88/6
Yes, sorry. I hav changed it and, yes your code does it well, but when you do: console.log(tableCode.html()); It is only printing one spam
|
0

Do this:

var jqueryObj=$("<table><tr><td><span class='thisone'>'the_text_to_change'</span><span class='thisone'>'other_text_to_change'</span><span class='thisone'>'another_text_to_change'</span></td></tr></table>");
jqueryObj.find('span').each(function(){
$(this).text($(this).attr("class"));
});

Fiddle Demo

2 Comments

This code is only filtering by span and not by class name
This is working: var jqueryObj=$("<table><tr><td><span class='thisone'>'the_text_to_change'</span><span class='thisone'>'other_text_to_change'</span><span class='thisone'>'another_text_to_change'</span></td></tr></table>"); jqueryObj.find('span.thisone').each(function(){ $(this).text('someText'); }); But if i put the variable like this: var jqueryObj=$("<tr><td><span class='thisone'>'the_text_to_change'</span><span class='thisone'>'other_text_to_change'</span><span class='thisone'>'another_text_to_change'</span></td></tr>"); Without table tag, it is only changing the first span. Why?

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.