hhh
hhh

Reputation: 52820

How to replace Div -content in a certain Class with jQuery?

<html>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script> 
        $("div.block").each(index){
            this.text('Fill this writing of divs with the classname "block"')} 
</script>

<body>
        <div>Not here</div> 
        <div class='block'>replace me -- not working, why?</div> 
        <div>Not here</div> 
        <div class='block'>replace me -- not working, why?</div>
</body>

</html>

Upvotes: 0

Views: 4036

Answers (1)

Adam Rackis
Adam Rackis

Reputation: 83358

You don't need to use each - you can just call the text method on your entire selection:

$("div.block").text('Fill this writing of divs with the classname "block"');

Incidentally, your problem above was that you were using each incorrectly

$("div.block").each(function(index, el){
    $(el).text('Fill this writing of divs with the classname "block"');
}); 

Upvotes: 4

Related Questions