RatDon
RatDon

Reputation: 3543

jquery returning name and value of radio button on click event

when a user clicks on a radio button, i want to set the value of a textbox in this fiddle with the name and value of the radio button which is clicked.

my script is:

$(function () {
    $("#radio").buttonset();
    $("radio").click(function () {
        $("#did").val = ($(this).attr('name') + "=" + $(this).val);
    });
});

where #did is the input textbox. but my code isn't doing that. please help.

Upvotes: 0

Views: 446

Answers (3)

Sterling Archer
Sterling Archer

Reputation: 22395

You're using .val() like a variable, you cannot assign to it. it's a function, call it!

Also, $("radio") looks for the html tag <radio>

So for example:

$("#did").val(($(this).attr('name') + "=" + $(this).val()));

Upvotes: 1

Ken Herbert
Ken Herbert

Reputation: 5236

Two issues as pointed out already, but both of which are causing your code to not work:

1) You are using .val as a property but it is a function. Thus you need to change this line

$("#did").val = ($(this).attr('name') + "=" + $(this).val);

to

$("#did").val($(this).attr('name') + "=" + $(this).val());

2) You are binding the click event to an element that doesn't exist (<radio>). You need to change your selector to something like input[type='radio'] or input[name='radio'].

Here is a working jsFiddle which implements both of these changes.

Upvotes: 1

ced-b
ced-b

Reputation: 4065

this refers to the global context. You have to user $(event.target) instead.

$("radio").click(function (aEvent) {
    $("#did").val ($(aEvent.target).attr('name') + "=" + $(aEvent.target).val());
});

Upvotes: -1

Related Questions