2

I have a radio button that looks like this:

<input class="MCQRadio" id="MCQ" mcq-id="1" name="MCQ" question-id="1" type="radio" value="MCQ" /> MCQ

[EDIT]

When i click the radiobutton, and several radio buttons have the same class name, how do i retrive the question-id value and mcq-id value from the specific button? Preferably with jQuery.

I have already set up a click handler like this:

$(document).ready(function () {

    $('.MCQRadio').click(function () {
1
  • 1
    I tried but i did not know the right words to search for, therefor the results were not useful :( Commented Aug 17, 2011 at 16:17

5 Answers 5

3
$('#MCQ').attr('question-id')
$('#MCQ').attr('mcq-id')
Sign up to request clarification or add additional context in comments.

Comments

3
var mcq-id = $('#MCQ').attr("mcq-id");
var question-id = $('#MCQ').attr("question-id");

3 Comments

I wouldn't use a class based selector here given that you have an ID available and he's not given any indication that this element is the only one on the page using that class.
Fair point, but he didn't specify that he didn't want us to use the class. But you're right.
I have edited my questions. Several radio buttons have the same class name. I need to get the value from a the specific radiobutton when i click on it.
0

Most of the answers given already are correct. I would make one addition to avoid traversing the DOM twice to find your radio button element.

//Store the jQuery object in a variable so that it's only retrieved once
var $myRadio = $('#MCQ');

//Get your attributes
var mcq-id = $myRadio.attr("mcq-id");
var question-id = $myRadio.attr("question-id");

You could also attach to the click event of all the radio buttons (by class selector) and grab the attributes in the event handler:

$('.MCQRadio').click(function() {
    //Use $(this) to grab the attribute value of the selected element
    var mcq-id = $(this).attr('question-id');
    var question-id = $(this).attr('mcq-id');
});

1 Comment

This is the answer i was looking for. Although i dont think you can use "-" in variable names, so i changed it to mcqid and questionid. Thanks.
0

The other answers already show you how to get the particular attributes. But since you added in your edit that you want to get the attributes from the particular one that was clicked you can try:

$('.MCQRadio').click(function() {

   $(this).attr('question-id');
   $(this).attr('mcq-id');

});

Comments

-1

These will return the values you want.

$('#MCQ').attr('mcq-id')
$('#MCQ').attr('question-id')

2 Comments

No, it won't. That will look for an element with ID=MCQRadio. If you want to select by class name you would use a '.' and not an '#'.
This won't work. In the example above, there is no element with an ID of "MCQRadio".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.