user172071
user172071

Reputation: 65

.replaceWith replace to function

I'm trying to use .replaceWith to replace words to a function but it's not working I think the problem is that I didn't right the function correct but I don't know how to write it right. Can Anybody help me (it's only a miniscule.) Thank you.

    <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.min.js">  
<html>
<body>

<script type="text/javascript">

var d=new Date();
var weekday=new Array(7);
weekday[0]="Monday";
weekday[1]="Tuesday";
weekday[2]="Wednesday";
weekday[3]="Thursday";
weekday[4]="Friday";
weekday[5]="Monday";
weekday[6]="Monday";


</script>


<script type="text/javascript">

$(document).ready(function($){  
    $('div.Shipby').replaceWith(function(){
        return $("<div>").text("Orders placed now will ship " + weekday[d.getDay()]);
    }); 
}); 

</script>

    <div class="Shipby">
         Tomorrow.
    </div>
</body>
</html>

Upvotes: 1

Views: 250

Answers (2)

James Johnson
James Johnson

Reputation: 46047

As Blender said, it sounds like it would be easier to just update the text or html, but if you want to use replaceWith, you can do it like this:

$(document).ready(function($){  
    $("div.Shipby").replaceWith(function(){
        return $("<div>").text("Orders placed now will ship " + weekday[d.getDay()]);
    }); 
}); 

Here's a jsFiddle: http://jsfiddle.net/TbE6P/

Upvotes: 2

Blender
Blender

Reputation: 298106

Just set the text():

$('div.Shipby').text("Orders placed now will ship " + weekday[d.getDay()]);

Or the html() if you plan on including HTML in your string:

$('div.Shipby').html("Orders placed now will ship " + weekday[d.getDay()]);

There's no need for using document.write().

Upvotes: 6

Related Questions