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?
jQuery text() Method Tip: To set or return the innerHTML (text + HTML markup) of the selected elements, use the html() method.
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.
$(function() { alert($("body"). find("a p"). text()); //or just $("a p"). text() });
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.
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');
$('#error').text('Test message here');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With