Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery - appending when radio button is checked

I have some radio buttons.

<div class="user">
        <input type="radio" class="user-radio" name="user" value="user"/>
        <p>BLABLA</p>
</div>
<div class="user">
        <input type="radio" class="user-radio" name="user" value="user"/>
        <p>BLABLA</p>
</div>
<div class="user">
        <input type="radio" class="user-radio" name="user" value="user"/>
        <p>BLABLA</p>
</div>

What I'm trying to do is, appending the <div class="user"> with an <a><a/> when the radio button inside that div is checked. And when i choose another radio button, I want to remove the last inserted <a><a/> and insert a new one to the newly selected radio button's parent div.

I've tried something like this but I couldn't get it to work:

<script>
$('.user').click(function () {
    if (!$(".user-radio").is(':checked')) {
        $(this).append('<a></a>');
    }
})
</script>
like image 935
archvile Avatar asked Feb 20 '23 16:02

archvile


1 Answers

See DEMO

$('.user-radio').change(function () {
    $('.user a').remove();
    $('.user-radio:checked').parent().append('<a>hello</a>');
});​
like image 137
Danil Speransky Avatar answered Feb 26 '23 23:02

Danil Speransky