
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);
Query provides two methods for working with slots in Web Components: getSlot
and setSlot
. These methods work on both slot elements and web component elements.
The getSlot
method gets the content of slots in Web Components.
// From a component (default slot)$(webComponent).getSlot()
// From a slot element$(slot).getSlot()
$(webComponent).getSlot(slotName)
Name | Type | Description |
---|---|---|
slotName | string | The name of the slot to access |
The combined HTML content of all elements assigned to the slot(s).
// Get all slotted content from a web component's default slotconst myComponent = document.createElement('my-component');// ... add some slotted content ...const allSlottedContent = $(myComponent).getSlot();
// Get content for just the "header" slotconst headerContent = $(myComponent).getSlot('header');
// Get the content of a slot elementconst slotContent = $(element.shadowRoot).find('slot[name="header"]').getSlot();
The setSlot
method sets the content of slots in Web Components.
$(webComponent).setSlot(content)
Name | Type | Description |
---|---|---|
content | string or Element | The content to add to the default slot |
$(webComponent).setSlot(slotName, content)
Name | Type | Description |
---|---|---|
slotName | string | The name of the slot to add content to |
content | string or Element | The content to add to the named slot |
Query object (for chaining).
// Set content for the default (unnamed) slot$(myComponent).setSlot('<p>Default content</p>');
// Set content for a named slot$(myComponent).setSlot('header', '<h1>Header content</h1>');
// Set content for a slot element (adds to parent component)$(element.shadowRoot).find('slot').setSlot('<div>New content</div>');
class MyComponent extends HTMLElement { constructor() { super(); this.attachShadow({ mode: 'open' }); this.shadowRoot.innerHTML = ` <div> <slot name="header">Default Header</slot> <slot>Default Content</slot> </div> `; }
// Helper to set default content if none provided setDefaultSlot(slotName, content) { // Check if slot is empty if (slotName) { if (!this.querySelector(`[slot="${slotName}"]`)) { $(this).setSlot(slotName, content); } } else { if (!this.querySelector(':not([slot])')) { $(this).setSlot(content); } } }}
customElements.define('my-component', MyComponent);
// Usageconst component = document.createElement('my-component');document.body.appendChild(component);
// Set header slot content$(component).setSlot('header', '<h1>My Header</h1>');
// Set default slot content$(component).setSlot('<p>Main content</p>');
// Get all slotted contentconsole.log($(component).getSlot());