How to get a value of an element by name instead of ID

How could I get the value of an element via the attribute name instead of the ID. eg if I use by id it would be $('#id').val();


Use the name attribute selector:

$("input[name=nameGoesHere]").val();

$('[name=whatever]').val()

The jQuery documentation is your friend.


Use the name attribute selector:

$("input[name='nameGoesHere']").val();

It is a mandatory requirement to include quotes around the value, see: http://api.jquery.com/attribute-equals-selector/


To get the value, we can use multiple attributes, one of them being the name attribute. E.g

$("input[name='nameOfElement']").val();

We can also use other attributes to get values

HTML

<input type="text" id="demoText" demo="textValue" />

JS

$("[demo='textValue']").val();