Bug Tracker

Opened 13 years ago

Closed 13 years ago

Last modified 13 years ago

#842 closed feature (wontfix)

DOM manipulation: Add 'replace' method

Reported by: sebastien@… 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")

Change History (2)

comment:1 Changed 13 years ago by jquery@…

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

comment:2 Changed 13 years ago by brandon

Priority: majorminor
Resolution: wontfix
Status: newclosed
Note: See TracTickets for help on using tickets.