I have a div
:
<div id="shoppingBasket">
</div>
Using Javascript or Jquery how would I fill this with the default text:
you have no products in your shopping basket.
What I mean by default text is I would want to change the text after they clicked a product. so maybe the default text in a variable and then included in the div via JavaScript or Jquery, which would allow the variable to be manipulated.
Use the insertAdjacentText() method to append text to a div element, e.g. container. insertAdjacentText('beforeend', 'your text here') . The method inserts a new text node at the provided position, relative to the element it was called on.
Add text to DIV using appendChild() Method in JavaScript (Method 2) You can also use the appendChild() method in JavaScript to add text or contents to a <div>.
Yes, you can directly add content text into a div tag, although using p tags would be preferable in most circumstances. Save this answer.
You can do it using jQuery text() or javascript innerText
With jquery, using jQuery text()
$('#shoppingBasket').text("you have no products in your shopping basket.");
With Javascript, using innerText
document.getElementById("shoppingBasket").innerText = "you have no products in your shopping basket.";
With jQuery this would be:
$('#shoppingBasket').text("you have no products in your shopping basket.");
with regular javascript:
document.getElementById("shoppingBasket").innerHTML = "you have no products in your shopping basket.";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With