Developer
Developer

Reputation: 6350

Get the id of html page in jQuery?

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

Answers (3)

Milind Anantwar
Milind Anantwar

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

Ferrakkem Bhuiyan
Ferrakkem Bhuiyan

Reputation: 2783

try this

  $(document).ready(function(){
        var formId = $('form').attr('youId');
    });

Upvotes: 0

Simon Arsenault
Simon Arsenault

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

Related Questions