I have:
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
cu=$("#box1").val();
cur=xmlhttp.responseText;
cur=cur.replace(cu,'<strong>'+cur+'</strong>');
$(".box2").html(cur);
}
I try that, and the replace just doesn’t happen.
When I change cur to just have a value of normal text (cur='my name is....';) then it works fine.
I’ve tried changing the replace line to: cur=cur.replace(cu,'found it'); – still nothing
This is all in the same function as the where the AJAX request takes place…
Any ideas?
Your code works if you fix one issue that may be a copy and paste error. You are setting
.html(current_response), butcurrent_responseis never defined.http://jsfiddle.net/4LXWd/1/
But I doubt it works the way you want it to. If
$("#box1").val()isworldandxmlhttp.responseTextishello world hello world, your result will only replace the first instance of world, and it will replace it with the entire response string, so you’ll get:Is that what you want? If only replacing the first instance of the matched string is fine, you probably want this:
Edit: But to replace every instance of the matched string, you’ll need to use a regular expression:
Working demo: http://jsfiddle.net/4LXWd/2/