Use the querySelector method to get an element by data attribute, e.g. document. querySelector('[data-id="box1"]') . The querySelector method returns the first element that matches the provided selector or null if no element matches the selector in the document.
You can also get the ID of individual element when using class selector based on index starting from 0, for example, to get the ID of first element in a set of matched elements you can use $(". box"). get(0). id or $(".
The getElementById() is a DOM Method that returns the element that has the ID attribute with the specified value.
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();
This works fine .. here btnAddCat is button id
$('#btnAddCat').click(function(){
var eventCategory=$("input[name=txtCategory]").val();
alert(eventCategory);
});
//name directly given
<input type="text" name="MeetingDateFrom">
var meetingDateFrom = $("input[name=MeetingDateFrom]").val();
//Handle name array
<select multiple="multiple" name="Roles[]"></select>
var selectedValues = $('select[name="Roles[]"] option:selected').map(function() {
arr.push(this.value);
});
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