Yogesh Chauhan's Blog

How to change value of a span tag using a reference from another div using jQuery?

in jQuery on January 23, 2021

We can use jQuery ID selector and target the span inside any element.

With jQuery ID selector we can select a single element with the given id attribute.

For id selectors, jQuery makes use of the JavaScript function document.getElementById().

After getting the element, we can set it’s content using either .text() or .html() method.

Example

Few more Things to know about jQuery ID selector

We’ll get a jQuery object in return when we call jQuery() (you can also use $()) with an id selector as its argument. The object will contain a collection of either zero or one DOM element.

We’re aware of the basic HTML rule about id attribute. Each id must be used only once within the HTML document.

If by mistake, you have more than one element that has the same ID then jQuery will only select the first matched element in the DOM.

An HTML document that has more than one element with the same ID attribute is invalid.

While using jQuery ID selector, you have to escape the characters with backslashes if the id contains characters like periods or colons.


Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 How to add Read More Read Less Button using JavaScript? #3 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #4 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #5 How to uninstall Cocoapods from the Mac OS? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
How does @extend rule work in SCSS (Sass)?SCSSWordPress: How to get field values in Advanced Custom Fields?WordPressConditional operator in JavaScript (aka ternary operator)JavaScriptHow to Install Xcode Command Line Tools on MacOS?MiscellaneousObservation of Human Behavior [Shopping Observation Example]MiscellaneousHow to add before after image effect using pure CSS?CSSJavaScript String fromCharCode() MethodJavaScriptLearn to create your skill bar using CSSCSSThe fundamental difference between HAVING and WHERE clauses in PostgresPostgresHow to detect the Blog Page in WordPress?WordPressCreate a simple bar chart and column chart using pure CSS gridCSSWordPress: How to add a Search Icon in Menus with toggle effect using jQuery?jQuery