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

175,406

Solution 1

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");
}

Solution 2

$("#message > span").text("your text");

or

$("#message").find("span").text("your text");

or

$("span","#message").text("your text");

or

$("#message > a.close-notify").siblings('span').text("your text");

Solution 3

Try this

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

function Errormessage(txt) {
    var elem = $("#message");
    elem.fadeIn("slow");
    // find the span inside the div and assign a text
    elem.children("span").text("your text");

    elem.children("a.close-notify").click(function() {
        elem.fadeOut("slow");
    });
}
Share:
175,406
bala3569
Author by

bala3569

Interested in software development, primary on the .NET Framework1 Email: [email protected]

Updated on May 23, 2021

Comments

  • bala3569
    bala3569 about 3 years

    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");
        });
    }