Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fill div with text

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.

like image 435
RSM Avatar asked May 25 '12 14:05

RSM


People also ask

How to put text into div?

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.

How to dynamically add text to a div?

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>.

Can a div contain text?

Yes, you can directly add content text into a div tag, although using p tags would be preferable in most circumstances. Save this answer.


2 Answers

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.";
like image 79
Adil Avatar answered Oct 06 '22 23:10

Adil


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.";
like image 41
chucktator Avatar answered Oct 06 '22 23:10

chucktator