Get attribute name value of <input>

Jquery

Jquery Problem Overview


How to get the attribute name value of a input tag using jQuery. Please help.

<input name="xxxxx" value="1">

Jquery Solutions


Solution 1 - Jquery

Give your input an ID and use the attr method:

var name = $("#id").attr("name");

Solution 2 - Jquery

Use the attr method of jQuery like this:

alert($('input').attr('name'));

Note that you can also use attr to set the attribute values by specifying second argument:

$('input').attr('name', 'new_name')

Solution 3 - Jquery

var value_input = $("input[name*='xxxx']").val();

Solution 4 - Jquery

While there is no denying that jQuery is a powerful tool, it is a really bad idea to use it for such a trivial operation as "get an element's attribute value".

Judging by the current accepted answer, I am going to assume that you were able to add an ID attribute to your element and use that to select it.

With that in mind, here are two pieces of code. First, the code given to you in the Accepted Answer:

$("#ID").attr("name");

And second, the Vanilla JS version of it:

document.getElementById('ID').getAttribute("name");

My results:

  • jQuery: 300k operations / second
  • JavaScript: 11,000k operations / second

You can test for yourself here. The "plain JavaScript" vesion is over 35 times faster than the jQuery version.

Now, that's just for one operation, over time you will have more and more stuff going on in your code. Perhaps for something particularly advanced, the optimal "pure JavaScript" solution would take one second to run. The jQuery version might take 30 seconds to a whole minute! That's huge! People aren't going to sit around for that. Even the browser will get bored and offer you the option to kill the webpage for taking too long!

As I said, jQuery is a powerful tool, but it should not be considered the answer to everything.

Solution 5 - Jquery

You need to write a selector which selects the correct <input> first. Ideally you use the element's ID $('#element_id'), failing that the ID of it's container $('#container_id input'), or the element's class $('input.class_name').

Your element has none of these and no context, so it's hard to tell you how to select it.

Once you have figured out the proper selector, you'd use the attr method to access the element's attributes. To get the name, you'd use $(selector).attr('name') which would return (in your example) 'xxxxx'.

Solution 6 - Jquery

A better way could be using 'this', it takes whatever the name of the 'id' is and uses that. As long as you add the class name called 'mytarget'.

Whenever any of the fields that have target change then it will show an alert box with the name of that field. Just cut and past whole script for it to work!

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js" type="text/javascript"></script>
<script>
$(document).ready(function() {
 $('.mytarget').change(function() {
var name1 = $(this).attr("name");
alert(name1);
 });
});
</script>

Name: <input type="text" name="myname" id="myname" class="mytarget"><br />
Age: <input type="text" name="myage" id="myage" class="mytarget"><br />

Solution 7 - Jquery

var theName;

theName = $("input selector goes here").attr("name");

Solution 8 - Jquery

using value get input name

 $('input[value="1"]').attr('name');

using id get input name

 $('input[class="id"]').attr('name');
   $('#id').attr('name');

using class get input name

 $('input[value="classname"]').attr('name');
   $('.classname').attr('name');  //if classname have unique value

Solution 9 - Jquery

If you're dealing with a single element preferably you should use the id selector as stated on GenericTypeTea answer and get the name like $("#id").attr("name");.

But if you want, as I did when I found this question, a list with all the input names of a specific class (in my example a list with the names of all unchecked checkboxes with .selectable-checkbox class) you could do something like:

var listOfUnchecked = $('.selectable-checkbox:unchecked').toArray().map(x=>x.name);

or

var listOfUnchecked = [];
$('.selectable-checkbox:unchecked').each(function () {
    listOfUnchecked.push($(this).attr('name'));
});

Solution 10 - Jquery

Pass class to input tag and access the name value by using class.

<input class="test-class" name='xxxxx' value=1>

<script>
    $('.test-class').attr('name');
</script>

Or you can also access the value using id.

<input id="test-id" name='xxxxx' value=1>
    
<script>
    $('#test-id').attr('name');
</script>

Solution 11 - Jquery

For the below line, Initially faced problem with out giving single code that 'currnetRowAppName'value is not taking space with string. So, after that giving single code '"+row.applicationName+"', its taking space also.

Example:

<button class='btn btn-primary btn-xs appDelete' type='button' currnetRowAppName='"+row.applicationName+"' id="+row.id+ >

var viewAppNAME = $(this).attr("currnetRowAppName");
alert(viewAppNAME)

This is working fine.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKarthikView Question on Stackoverflow
Solution 1 - Jquerydjdd87View Answer on Stackoverflow
Solution 2 - JquerySarfrazView Answer on Stackoverflow
Solution 3 - JqueryLaura RieraView Answer on Stackoverflow
Solution 4 - JqueryNiet the Dark AbsolView Answer on Stackoverflow
Solution 5 - Jqueryuser229044View Answer on Stackoverflow
Solution 6 - JqueryMikeys4uView Answer on Stackoverflow
Solution 7 - JqueryPaddyView Answer on Stackoverflow
Solution 8 - JqueryniraliView Answer on Stackoverflow
Solution 9 - JqueryAntonio CorreiaView Answer on Stackoverflow
Solution 10 - JquerySheetal MehraView Answer on Stackoverflow
Solution 11 - JquerySenthil Kumar SKView Answer on Stackoverflow