Reputation: 51
I have a few name elements in a html document that i want to pass to the jquery function assigned to a variable named "elements"
I want to assign the value of the clicked elements ID to the variable x.
I am new to JQuery can somebody help me please.
var elements = document.getElementsByName('color');
$(elements).click(function() {
x = $(this).id;
});
Upvotes: 0
Views: 40
Reputation: 3527
Try this:
$('html').on('click', '[name="color"]', function(){
var x = $(this).attr('id');
})
Upvotes: 0
Reputation: 174
You dont want to use jquery with javascript DOM Objects you've already gotten. You can grab the objects as Jquery objects with the class selector.
$('.color').click(function() {
x = this.id;
alert(x);
});
Here is a http://jsfiddle.net/2gmM3/
Upvotes: 0
Reputation: 4288
$(function(){
var x;
$('[name="color"]').on('click', function(){
x = $(this).attr('id');
alert(x);
});
});
Upvotes: 1