开发者

jQuery - Adding text to already dynamic text

开发者 https://www.devze.com 2023-02-25 05:59 出处:网络
I have a radio button as f开发者_运维百科ollows <input type=\"radio\" name=\"thisone\" value=\"yes\">

I have a radio button as f开发者_运维百科ollows

<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?


$(':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();
});


Try this one:

JQUERY:

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

CLICK HERE TO SEE THE DEMO

0

精彩评论

暂无评论...
验证码 换一张
取 消