Reputation: 3653
I have the following code:
<div id="container">
<input type="text" name="a1" id="a1">
<input type="text" name="a2" id="a2">
</div>
And I want to replace all instance of the text "a" to "b" for the id and name property for all the element inside the div id="container"
so the new code should be like this:
<div id="container">
<input type="text" name="b1" id="b1">
<input type="text" name="b2" id="b2">
</div>
I just can't seem to be able to make it work using the javascript replace().
Upvotes: 1
Views: 5370
Reputation: 1831
Here
$(function(){
var html =$('#container').html().replace(/a/g,'b');
$('#container').html(html);
});
Upvotes: 0
Reputation: 3950
$("#container").children("input").each(function() {
var name = $(this).attr("name");
var id = $(this).attr("id");
name = name.replace(/a/g, "b");
id = id.replace(/a/g, "b");
$(this).attr("name", name);
$(this).attr("id", id);
});
Upvotes: 0
Reputation: 79
$('#a1').attr('name', 'b1').attr('id', 'b1');
$('#a2').attr('name', 'b2').attr('id', 'b2');
Upvotes: 1
Reputation: 227200
$('#container input').each(function(){ // Loop through all inputs
this.name = this.name.replace('a', 'b'); // Replace name
this.id = this.id.replace('a', 'b'); // Replace ID
});
DEMO: http://jsfiddle.net/G3vCf/
Upvotes: 6