Select an element by name with jQuery

August 09, 2014 | Last tested: June 2015 | 643 views | Comments

Select an element by it's name using jQuery.

For Example.

Select a specific element through it's attribute name.

Requirement:

  • Display the attribute ID of the selected element on the console.

jQuery code:

$(document).ready(function(){
	$("[name='language']").click(function(){
		console.log($(this).attr("id"));
	});
});

HTML code:

<div>
	<p>What do you like to code best:</p>
	<p>JAVA <input type="radio" name="language" id="JAVA"></p>
	<p>PHP <input type="radio" name="language" id="PHP"></p>
	<p>Javascript <input type="radio" name="language" id="Javascript"></p>
	<p>jQuery <input type="radio" name="language" id="jQuery"></p>
</div>

 

Live Demo:

Open your console to see the result.

What do you like to code best:

JAVA

PHP

Javascript

jQuery

Related Tutorials


Must Read