Side navigation
#842 closed feature (wontfix)
Opened January 17, 2007 08:31PM UTC
Closed January 31, 2007 05:22PM UTC
Last modified June 19, 2007 08:13AM UTC
DOM manipulation: Add 'replace' method
Reported by: | sebastien@project-oj | Owned by: | |
---|---|---|---|
Priority: | minor | Milestone: | |
Component: | core | Version: | |
Keywords: | dom manipulation replace | Cc: | |
Blocked by: | Blocking: |
Description
The replace method could be used to replace the actual content of an element. Suppose I have an HTML code like this:
<div> <span class="username label">USERNAME</span> <span class="useraddress label">USERADDRESS<span> </div>
and I want to replace the content of the ".username.label
" and ".useraddress.label
" by some JavaScript-generated value. It would be really cool to have a replace
function that could be used as:
$(".username.label").replace("John Smith") $(".useraddress.label").replace("john.smith@yahoo.com")
Javascript already has a native replace() method - it invokes the regular expression engine on a string, and could very easily be used in your scenario (although not as elegant). There might be a more efficient/elegant way to do this with jQuery, however I am still very new to this myself.
For a single-item use:
$('.username.label').text($('.username.label').text().replace(/USERNAME/,'John Smith'));
For n items use:
$('.username.label').each(function()
{
$(this).text($(this).text().replace(/USERNAME/,'John Smith'));
});
}
More info on the JS regex engine can be found here: http://www.regular-expressions.info/javascript.html