1

I have a question on how to use Jquery UI.

This is my first time that I try Jquery UI and I could add a button by using Jquery UI button.

Now, I'd like to add events when the radio is switched on and off.

For example, when the radiobox is turned on, an alert window shows up saying "on" and when the radiobox is turned off, an alert window shows up saying "off".

How could I add events to Jquery UI button?

Thanks in advance !!

output

Jquery UI button

javascript

$(function(){

    $('input[type=radio]').button();
    $('.set').buttonset();

})

html

<div class="set">
    <input type="radio" name="radio" id="radio1"><label for="radio1" />ON</label>
    <input type="radio" name="radio" id="radio2"><label for="radio2" />OFF</label>
</div>

3 Answers 3

3

You can try like this

$('.set input').on('click', function(){
   alert($("label[for='"+$(this).attr("id")+"']").text()); 
});

fiddle http://jsfiddle.net/DFKdh/

Sign up to request clarification or add additional context in comments.

3 Comments

Oh I see. It's getting access to the text of label tag directly. This one also works! Thank you very much!
Yup, so you don't have to pay attention to the label positioning
Thank you very much. I accepted another answer because he was faster. But I upvoted your answer. Thanks again!
2
$('.set input[type=radio]').change(function () { 
    //doSomething
})

or

$('.set').on('change', 'input[type=radio]', function () { 
    if ($(this).is(':checked'))
        alert($(this).text());
})

Comments

1

Try this, this is helpful for you. I also suggest you try use <span> tag always for html text,

Html

<div class="set">
    <input type="radio" name="radio" id="radio1"><label for="radio1" />ON</label>
    <input type="radio" name="radio" id="radio2"><label for="radio2" />OFF</label>
</div>

JS

$("input[type=radio]").change(function() {  
            alert($(this).next('label').html());
        });

1 Comment

I need to wait for one more minute to accept an answer. wait a minute.

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.