Replacing A Part Of Text Inside A Td
I have a td with this format: Business Owner - Delivery Director me@business.com How can
Solution 1:
You can use the childNodes
property to select the first text node and change its value:
$('td')[0].childNodes[0].nodeValue = "New content";
Here is a demonstration: http://jsfiddle.net/dpyNy/
Solution 2:
Use .contents()
to get the text node (with CharacterData
interface) and then manipulate its content:
$("td").contents()[0].data = "Businees Owner (CEO)";
Solution 3:
With that structure, you'll have to do string manipulation, either using split
:
var td = $("selector_for_the_td_element");
var text = td.html().split("<br>");
text[0] = "new content";
td.html(text.join("<br>"));
...or using indexOf
and substring
:
var td = $("selector_for_the_td_element");
var text = td.html();
var index = text.indexOf("<br>");
text = "new content" + text.substring(index);
td.html(text);
Now, if you can change your HTML structure to this:
<td><span>Business Owner - Delivery Director</span><br><ahref="mailto:me@business.com">me@business.com</a></td>
...it gets a lot easier:
$("selector_for_the_td_element > span").text("new content");
Solution 4:
If the html structure is exactlly what you post, you can achieve it like this:
this.childNodes[0].nodeValue = 'hello'; //this is the td element
Post a Comment for "Replacing A Part Of Text Inside A Td"