How to get and set the content of div tag html with Jquery

Dung Do Tien Apr 10 2020 435

I have a div tag container HTML, this div content an article detail and I want to get current content or set some other article detail to this div. How can I do it?

<div class="col-md-2 content-article-detail">
    // set or get content of this div
</div> 
Have 1 answer(s) found.
  • S

    Sandeep Kumar Apr 12 2020

    Jquery has provided two methods to get content or text of a container tag

    1. text() : Get or set only text for HTML container tags.

    2. html() : Get or set both text and HTML tag for HTML container tags.

    See an example below

    <div class="col-md-2 content-article-detail">
        <p>This is content of article</p>
    </div> 
    var content = $(".content-article-detail").text();
    // return content = "This is content of article";
    
    AND
    
    var content = $(".content-article-detail").text(); 
    // return content = "This is content of article"; 

    See a demo here

    *Note: The two methods have different uses, you should consider which one is suitable for your purpose.

Leave An Answer
* NOTE: You need Login before leave an answer

* Type maximum 2000 characters.

* All comments have to wait approved before display.

* Please polite comment and respect questions and answers of others.

Popular Tips

X Close