Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use jquery to set value of div tag

Tags:

html

jquery

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.

like image 379
air Avatar asked Oct 15 '09 07:10

air


People also ask

Can we set value in div?

div elements don't have a . value property that would get submitted to the backend; use an input element for that.

How do I get text inside a div using jQuery?

To get the value of div content in jQuery, use the text() method. The text( ) method gets the combined text contents of all matched elements. This method works for both on XML and XHTML documents.

How do I set the value of a element in jQuery?

jQuery val() Method The val() method returns or sets the value attribute of the selected elements. When used to return value: This method returns the value of the value attribute of the FIRST matched element.


1 Answers

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.

like image 98
farzad Avatar answered Sep 23 '22 05:09

farzad