I am trying to get a checkbox name when a checkbox is clicked...i am using a javascript function but it returns undefined.
Here is the code:
notType= document.getElementById($('[type="checkbox"]').attr('id')).value;
alert(notType);
I am trying to get a checkbox name when a checkbox is clicked...i am using a javascript function but it returns undefined.
Here is the code:
notType= document.getElementById($('[type="checkbox"]').attr('id')).value;
alert(notType);
This should work. $("#" + id) finds the element with the specified id. After that, you get the attribute called "name".
var notType = $("#" + id).attr("name");
Pure JavaScript way of getting the checkbox name attribute when clicked:
document.querySelector('input[type="checkbox"]').addEventListener('click', ({ target }) => {
const name = target.getAttribute('name');
...
});
JQuery way of getting the checkbox name attribute when clicked:
$('input[type="checkbox"]').on('click', function() {
const $checkbox = $(this);
const name = $checkbox.attr('name');
...
});