Leon
Leon

Reputation: 745

JS - replaceWith not working

I'm trying to update a div with a static value but replaceWith seems to be less collaborative than I was thinking.
This is the function:

updatePeopleNumber: function(peopleNumber) {
   var numberItem = jQuery("#visitors-container");
   numberItem.find("vn").replaceWith(peopleNumber);
}

Again: peopleNumber is a static value set right before this function is called. This is the HTML:

<div id="row-2" class="row">
        <div id="visitors-container">
            <div id="visitors-number" class="vn"><?php echo getPeopleNumber(); ?></div>
            <div id="visitors-text">visitors today</div>
        </div>
</div>

The PHP code is there for security reasons: if no value is set, then the PHP comes in. But I need the js code to replace the value in the div with peopleNumber...
If someone is wondering: no, deleting the PHP code doesn't solve the problem...

Upvotes: 0

Views: 1923

Answers (3)

Klevis Cipi
Klevis Cipi

Reputation: 37

You can do :

<div id="row-2" class="row">
    <div id="visitors-container">
        <div id="visitors-number" class="vn"><span><?php echo getPeopleNumber(); ?></span></div>
        <div id="visitors-text">visitors today</div>
    </div>
 </div>

JavaScript

$('#visitors-number').find("span").text(peopleNumber);

Upvotes: 0

Yogesh Mistry
Yogesh Mistry

Reputation: 2152

You have error in your selector at find("vn"). You forgot the class selector ..

updatePeopleNumber: function(peopleNumber) {
   var numberItem = jQuery("#visitors-container");
   numberItem.find(".vn").replaceWith(peopleNumber);
}

Upvotes: 0

Vladu Ionut
Vladu Ionut

Reputation: 8193

Whereas you don't use the returned value of the replaceWith method you can use instead of it the jquery.html setter

  numberItem.find(".vn").html(peopleNumber);

Also you missed the dot for the class name numberItem.find(".vn")

Upvotes: 2

Related Questions