I would like the label associated with a radio button ‘hot’. I started to implement this using the .siblings() method. I think there must be a better way. The click event on the radio button looks like this:
$(".RadioButton").click(function(event) {
var questionId = $(this).find('input').attr('name');
var responseId = $(this).find('input').attr('value');
var answerText = displayPopupQuizAnswer($(this));
This works great. I would like the same code to execute when the user clicks on the text label accompanying the radio button. The html looks something like this:
<div class="answers">
<span class="radiobutton>
<input type="radio" name="answer1"/>
</span>
<span class="answertextwrapper">
<a href="return false">this is the text</a>
</span>
</div>
This is simplified but it’s close. What I want is to capture the click event on the element with class=”answertextwrapper” i.e. $(“.answerwrapper”).click
So I need to somehow reference the input when the text is clicked. Make sense?
Any ideas?
Simple, use actual label elements;
When you use these, not only do you gain nice usability, but their click event is bound to the radio button’s click event. In otherwords, you don’t have to do any additional jQuery, just update your HTML.
Here it is in action – if you have firebug you can clearly see that $(this) always refers to the
<input>, regardless of whether or not you actually click on the corresponding<label>