Dan
Dan

Reputation: 1719

Replace contents of DIV on page load with jquery

Using jquery is there a way I can take the contents of a DIV and replace it with something else when the page loads?

<div class="content">Before</div>

to

<div class="content">After</div>

Upvotes: 3

Views: 10684

Answers (3)

Ashar Zafar
Ashar Zafar

Reputation: 402

**Replace contents of DIV on page load with jquery in worpdress 1st one replace DIV and 2nd replace just text Also Onload jQuery String Replace **

    <script type="text/javascript"> 

    jQuery(function($){ 
    $( ".yourclass " ).replaceWith( "<h6>New heading</h6>" );
     });

    </script>
 **OR For text replace**

    <script type="text/javascript"> 
    jQuery(function($){ 
    $( ".single_add_to_cart_button " ).text( "Sepete ekle" );
     });
    </script>


/* For String Replace */

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript"> 
$(document).ready(function() {
    var strNewString = $('body').html().replace(/\On hold/g,'Processing');
    $('body').html(strNewString);
});


EXAMPLE : 2

jQuery("[lang='fr-FR']  .label-search option").each(function() {
var text = jQuery(this).text();
text = text.replace("All Category", "Catégories ");
jQuery(this).text(text);
});
</script>

Upvotes: 0

Don Pavlik
Don Pavlik

Reputation: 136

To piggyback off of Curt's answer the full implimentation for this would be this:

$(document).ready( function() {
    $(".content").text("After");
});

This will execute when the window has been loaded and update the inner text of the desired div element.

Upvotes: 3

Curtis
Curtis

Reputation: 103388

Use text():

$(".content").text("After");

Live Demo: http://jsfiddle.net/heY3j/

Upvotes: 5

Related Questions