Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery add text to span within a div

Tags:

jquery

<div id="tagscloud">        <span></span> </div> 

How would I do to add some text within the span like code below ?

<span>**tag cloud**</span>

Edit: actually the span has an id

<div id="tagscloud"> <span id="WebPartCaptionWPQ2"></span> </div> 
like image 821
user472285 Avatar asked Feb 03 '11 08:02

user472285


People also ask

How do you use span tags?

The <span> tag is an inline container used to mark up a part of a text, or a part of a document. The <span> tag is easily styled by CSS or manipulated with JavaScript using the class or id attribute. The <span> tag is much like the <div> element, but <div> is a block-level element and <span> is an inline element.


2 Answers

You can use:

$("#tagscloud span").text("Your text here"); 

The same code will also work for the second case. You could also use:

$("#tagscloud #WebPartCaptionWPQ2").text("Your text here"); 
like image 74
kgiannakakis Avatar answered Sep 18 '22 15:09

kgiannakakis


Careful - append() will append HTML, and you may run into cross-site-scripting problems if you use it all the time and a user makes you append('<script>alert("Hello")</script>').

Use text() to replace element content with text, or append(document.createTextNode(x)) to append a text node.

like image 36
Scarabeetle Avatar answered Sep 16 '22 15:09

Scarabeetle