
Enter a search term above to see results...
Enter a search term above to see results...
Content methods in Query allow you to manipulate content accessing form data, text and html.
Gets or sets the value of form elements.
$('input').value();
$('input').val(newValue)
Name | Type | Description |
---|---|---|
newValue | any | The new value to set |
value
is also available as val
$('input').val(); // identical;
Query object (for chaining).
The value of the first element in the set.
// Get the value of an inputconst inputValue = $('input').val();console.log(inputValue);
// Set the value of an input$('input').val('New value');
Gets or sets the text content of elements.
$('p').text()
$('p').text(newText)
Name | Type | Description |
---|---|---|
newText | string | The new text content to set |
The combined text content of all elements in the set.
Query object (for chaining).
// Get the text of an elementconst buttonText = $('button').text();console.log(buttonText);
// Set the text of an element$('button').text('Click me!');
$('.container').html()
$('.container').html(newHtml)
Name | Type | Description |
---|---|---|
newHtml | string | The new HTML content to set |
The HTML content of the first element in the set.
Query object (for chaining).
// Get the HTML contentconst containerHtml = $('.container').html();console.log(containerHtml);
// Set the HTML content$('.container').html('<p>New content</p>');
Difference from HTML Outerhtml returns the element including its own tag
$('.element').outerHTML()
$('.element').outerHTML(newHtml)
Name | Type | Description |
---|---|---|
newHtml | string | The new outer HTML content to set |
Query object (for chaining).
// Get the outer HTMLconst elementOuterHtml = $('.element').outerHTML();console.log(elementOuterHtml);
// Set the outer HTML$('.element').outerHTML('<div class="new-element">Replaced content</div>');
Versus text()
text
will return text content recursively of nested nodes whiletextNode
will only return the text content of the selected node, in some cases this distinction can be essential.
$('p').textNode()
The combined text content of immediate text node children.
// Get only the immediate text content, ignoring nested elementsconst immediateText = $('p').textNode();console.log(immediateText);