Reputation: 6350
I have an id on my page from that I have taken all the html by using .html()
function in a variable .Now, I want to use the id from the variable in which I have stored the html. How can I do this in jQuery? I am not getting any idea. Please help me in this!
var idhtml= $("#id").html();
Like this, I have the html in idhtml
<input type="text" id="id1" name="id1" value="" class ="test" />
<input type="text" id="id2" name="id1" value="" class ="test" />
<input type="text" id="id3" name="id1" value="" class ="test" />
By idhtml
, I want to get the id in the variable.
Upvotes: 0
Views: 117
Reputation: 82241
You can use .each()
.
To set the attribute you can use
1) .attr('attribute_name','attribute_value')
or
2) .data('attribute_name','attribute_value')
with appropriate selector.
$("[name='id1']").each(function () {
console.log($(this).attr(id));
//to set the attr you can use
$(this).attr('attribute_name','attribute_value');
//to set custom attribute, use .data()
$(this).data('attribute_name','attribute_value');
});
Upvotes: 1
Reputation: 2783
try this
$(document).ready(function(){
var formId = $('form').attr('youId');
});
Upvotes: 0
Reputation: 1845
You can get the IDs like this:
$("input[id^='id']").each(function () {
console.log(this.id);
// Change the id:
$(this).attr("id", "new_id");
});
Select every input
with an id that starts with id
and for each input returned, print its ID in the console.
Upvotes: 1