I know the name of HTML element but not id. How to fetch id using name of the element using Javascript. Kindly help.
var elements = document.getElementsByName( 'yourname' );
var id = elements[0].getAttribute( 'id' );
docu @ MDN
If you have multiple elements of that name, you will have to run though the array of elements
and pick the right one. If there is just one, the above code will work.
Sirko's way is correct. Just in case you (or anyone else) are interested, here's the jquery way of doing it:
alert($("*[name='foo']").attr('id'));
DEMO
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With