Reputation: 47774
I have html below. What I want to do depends on some conditions. I want to keep changing the text which is Sometitle
.
<div id="header">
Sometitle
<div>
<div>
aa
</div>
<div>
bb
</div>
</div>
</div>
I tried using Jquery below but it removes the other divs also.
$("#header").text("Its a thrusday today !");
How do I get this done? I cannot modify this HTML in any case. I just have to use jquery and get this thing done.
Here is a jsfiddle I created : http://jsfiddle.net/qYUBp/
Upvotes: 5
Views: 5282
Reputation: 4615
can you check this
headerClone = $("#header").clone();
$(headerClone).find('div').remove();
var currentText = $(headerClone).html().replace(/(\r\n|\n|\r|\t)/gm,"");
newHtml = $("#header").html().replace(currentText,"Gotcha");
$("#header").html(newHtml);
Upvotes: 0
Reputation: 2623
That text Sometitle
is an orphan node, you are going to want to wrap an element around it to properly manipulate it
<div id="header">
<span>Sometitle</span>
<div>
<div>
aa
</div>
<div>
bb
</div>
</div>
</div>
EDIT
But since you regrettably cannot change the html, you could do as the solution suggests in the other post. They had a typo that I corrected here:
var your_div = document.getElementById('header');
var text_to_change = your_div.childNodes[0];
text_to_change.nodeValue = 'new text';
check your updated JsFiddle
Upvotes: 3
Reputation: 225
for this case only, when rendering to browser
var new_s = "Its a thrusday today !";
var s = $("#header").children(":first").html();
$("#header").text(new_s+"<div>"+s+"</div>");
Upvotes: 0
Reputation: 3287
If you dont want it in a single line.
var tmp=$("#header>div").html();
$("#header").text("its thursday").append('<div>'+tmp+'</div>');
Upvotes: 3
Reputation: 1160
try this
$("#header").html($("#header").html().replace("Sometitle","Its a thrusday today !"));
Upvotes: 0