Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get span tag inside a div in jQuery and assign a text?

I use the following ,

<div id='message' style="display: none;">   <span></span>  <a href="#" class="close-notify">X</a> </div> 

Now i want to find the span inside the div and assign a text to it...

function Errormessage(txt) {     $("#message").fadeIn("slow");     // find the span inside the div and assign a text     $("#message a.close-notify").click(function() {         $("#message").fadeOut("slow");     }); } 
like image 501
bala3569 Avatar asked Apr 20 '10 06:04

bala3569


People also ask

How would you get the text written inside a span element?

Use the textContent property to get the text of a span element, e.g. const text = span. textContent . The textContent property will return the text content of the span and its descendants.

Can we write span inside div?

you can have span tag inside div tag. since span is an inline tag, by adding display : block or inline-block to span tag you can give it signifigance for its style to be visible.


1 Answers

Try this:

$("#message span").text("hello world!"); 

See it in your code!

function Errormessage(txt) {     var m = $("#message");      // set text before displaying message     m.children("span").text(txt);      // bind close listener     m.children("a.close-notify").click(function(){       m.fadeOut("slow");     });      // display message     m.fadeIn("slow"); } 
like image 165
maček Avatar answered Sep 17 '22 13:09

maček