Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set text in jquery

Tags:

jquery

If I want to set the text of a <div id="error"></div> to "Test message here", do I do:

 $('<div id="error">').text('Test message here'); 

I tried this and it's not working. Thoughts?

like image 602
sehummel Avatar asked Dec 22 '10 17:12

sehummel


People also ask

How do I set inner text in jQuery?

jQuery text() Method Tip: To set or return the innerHTML (text + HTML markup) of the selected elements, use the html() method.

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

JQuery val() method: This method return/set the value attribute of selected elements. If we use this method to return value, it will return the value of the FIRST selected element. If we use this method to set value, it will set one or more than one value attribute for set of selected elements.

How do I get paragraph text in jQuery?

$(function() { alert($("body"). find("a p"). text()); //or just $("a p"). text() });

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.


2 Answers

You create a new div and set its text, but you don't insert it anywhere. What you need to do is:

var el = $('<div id="error">').text('Test message here'); $(document).append(el); 

or, if the div is already there:

$("#error").text('Test message here'); 
like image 193
Gabi Purcaru Avatar answered Sep 19 '22 00:09

Gabi Purcaru


 $('#error').text('Test message here'); 
like image 41
Dejan Marjanović Avatar answered Sep 20 '22 00:09

Dejan Marjanović