BigJobbies
BigJobbies

Reputation: 4343

jQuery - Adding text to already dynamic text

I have a radio button as follows

<input type="radio" name="thisone" value="yes">

I then have a bit of text like so

<span class="iamtext">hey ya'll, im some text</span>

Now this text is being dynamically written in from the database, so assuming i dont know what the text is, how would i then add 'and im epic' to the end of that text only if someone clicks the radio button?

Upvotes: 2

Views: 344

Answers (2)

alex
alex

Reputation: 490233

$(':radio[name="yes"]').click(function() {
    $('.iamtext').text(function(index, oldText) {
        return oldText + ' and im epic';
    });
});

jsFiddle.

Update

Just one little thing, if there was a 'no' radio button next to that, is it possible to be able to delete that ' and im epic' text and revert back to the dynamic text that was there before without the addition?

Try this code...

$(':radio[name="yes"]').click(function() {
    $('.iamtext').append('<span> and im epic</span>');
});

$(':radio[name="no"]').click(function() {
    $('.iamtext').find('span').remove();
});

Upvotes: 1

Sukhjeevan
Sukhjeevan

Reputation: 3156

Try this one:

JQUERY:

$("input:radio").click(function(){
    var vTextTobeadded = "and im epic";
    $("span.iamtext").append(vTextTobeadded);
});

CLICK HERE TO SEE THE DEMO

Upvotes: 1

Related Questions