12 24 ...">

Updating the content of the entire span element with its own text

<div id="resultperpage">
    <span><a>12</a></span>
    <span><a>24</a></span>
    <span><a>48</a></span>
    <span><a>98</a></span>
</div>


$('#resultperpage span').click(function() {
    $('#resultperpage span').html($(?).html() + 'A'));
});

<div id="resultperpage">
    <span><a>12A</a></span>
    <span><a>24A</a></span>
    <span><a>48A</a></span>
    <span><a>98A</a></span>
</div>

      

Please help me replace question mark in jQuery function to output text + 'A'

+3


source to share


3 answers


You can use the callback function html()

instead to replace everything

$('#resultperpage span').click(function() {
    $('#resultperpage span').html(function(_, html) {
        return html + 'A';
    });
});

      



or replace only by clicking

$('#resultperpage span').click(function() {
    $(this).html(function(_, html) {
        return html + 'A';
    });
});

      

+2


source


JQuery

$("#resultperpage span a").each(function(){
    var txt= $(this).text();
    $(this).text(txt+"A");
});

      



DEMO

+1


source


you can use the each () function :

$('#resultperpage span').click(function() {
    if($(this).html().substr($(this).html().length-1)!='A'){
        $('#resultperpage span').each(function(){
            $(this).html($(this).html() + 'A');
        });
    }
});

      

DEMO

0


source







All Articles