javascript - Remove placeholders using jQuery -


i have following code , want remove placeholder.

<div class="myclass">   <p><a href="http:[sitename]">[sitetile]</a>[change too]someothercontent</p>  </div> 

and want change above markup event using jquery:

<div class="otherclass">   <p><a href="http:yahoo.com">changemaincontent</a>changesomeothercontent</p> </div> 

create object index named part want replace (inside brackets) , assign value it. use $.each-function iterate on object , replace values in html 1 object. after assign new html-string element.

var change = {     'sitename': 'yahoo.com',     'sitetile': 'changemaincontent',     'change too': 'change' };  var $elem = $('.myclass > p'); //cache element var html =$elem.html(); //get html-string  $.each(change, function(index, value){ //iterate on object       html =  html.replace('[' + index + ']', value); //replace values  });  $elem.html(html); //assign new html-string 

demo

reference

.replace()

$.each()

.html()


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -