Use jquery to set value of div tag

JqueryHtml

Jquery Problem Overview


I have this html div tag defined:

<div style="height:100px; width:100px" class="total-title">
    first text
</div>

I have jquery code to change its value:

 $('div.total-title').html('test');

But this does not change the content of the div.

Jquery Solutions


Solution 1 - Jquery

if your value is a pure text (like 'test') you could use the text() method as well. like this:

$('div.total-title').text('test');

anyway, about the problem you are sharing, I think you might be calling the JavaScript code before the HTML code for the DIV is being sent to the browser. make sure you are calling the jQuery line in a <script> tag after the <div>, or in a statement like this:

$(document).ready(
    function() {
        $('div.total-title').text('test');
    }
);

this way the script executes after the HTML of the div is parsed by the browser.

Solution 2 - Jquery

To put text, use .text('text')

If you want to use .html(SomeValue), SomeValue should have html tags that can be inside a div it must work too.

Just check your script location, as farzad said.

Reference: .html and text

Solution 3 - Jquery

try this function $('div.total-title').text('test');

Solution 4 - Jquery

You have referenced the jQuery JS file haven't you? There's no reason why farzad's answer shouldn't work.

Solution 5 - Jquery

When using the .html() method, a htmlString must be the parameter. (source) Put your string inside a HTML tag and it should work or use .text() as suggested by farzad.

Example:

<div class="demo-container">
    <div class="demo-box">Demonstration Box</div>
</div>

<script type="text/javascript">
$("div.demo-container").html( "<p>All new content. <em>You bet!</em></p>" );
</script>

Solution 6 - Jquery

use as below:

<div id="getSessionStorage"></div>

For this to append anything use below code for reference:

$(document).ready(function () {
        var storageVal = sessionStorage.getItem("UserName");
        alert(storageVal);
        $("#getSessionStorage").append(storageVal);
     });

This will appear as below in html (assuming storageVal="Rishabh")

<div id="getSessionStorage">Rishabh</div>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionairView Question on Stackoverflow
Solution 1 - JqueryfarzadView Answer on Stackoverflow
Solution 2 - JqueryMaLKaV_eSView Answer on Stackoverflow
Solution 3 - JqueryAlex HView Answer on Stackoverflow
Solution 4 - Jquerydjdd87View Answer on Stackoverflow
Solution 5 - JqueryJo SmoView Answer on Stackoverflow
Solution 6 - Jqueryrp4361View Answer on Stackoverflow