user3453366
user3453366

Reputation: 51

Parameters and JQuery

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

Answers (4)

Prosto Trader
Prosto Trader

Reputation: 3527

Try this:

$('html').on('click', '[name="color"]', function(){
   var x = $(this).attr('id');
})

Upvotes: 0

tgeery
tgeery

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

kosmos
kosmos

Reputation: 4288

$(function(){
    var x;
    $('[name="color"]').on('click', function(){
        x = $(this).attr('id'); 
        alert(x);
    });
});

Check jsFiddle

Upvotes: 1

RazvanDH
RazvanDH

Reputation: 782

$('#color').click(function() {
   x = $(this).attr('id');
});

Upvotes: 1

Related Questions