YogeshChauhan . com

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.

dreamhost

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to uninstall Cocoapods from the Mac OS? #4 How to add Read More Read Less Button using JavaScript? #5 How to Use SQL MAX() Function with Dates? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

May 5 Use inline if to make a shorter conditional syntax in React May 4 What is Conditional Rendering in React? May 3 How does @extend rule work in SCSS (Sass)? May 2 How different is Handling Events in React vs HTML DOM? May 2 How to create bouncing balls using HTML canvas and JavaScript? Apr 30 HTML canvas methods

You might also like these

How to select an element using its ID without the high specificity of the ID selector?CSSHow to Secure Web or Mobile Browsers?MiscWhat are Conditional Tags in WordPress?WordPressLearn to create your skill bar using CSSCSSHow to create a Star Ratings using CSS?CSSJavaScript Number MethodsJavaScriptHow to host Google fonts on your server and add them using CSS?CSSOOP, Class and Objects Strategies For Beginners (PHP)PHPGlobal JavaScript methods to convert variables to numbersJavaScriptLearn to create profile card using HTML and CSSCSSOverview of Drupal and how it worksDrupalMIN, MAX, COUNT, AVG and SUM in SQLSQL/MySQL