Get the App
SLTechnology News&Howtos  ›  Development  › 

How to change id with click in jquery

Shulou Source: shulou.com Published: 2022-05-31 17:58:51 09月18日 Update

In this article, the editor introduces in detail "jquery how to change id with click", the content is detailed, the steps are clear, and the details are handled properly. I hope this article "jquery how to change id with click" can help you solve your doubts.

Methods: 1, the use of "element object .click (function () {event handling function})" to add click events to the specified element, and specify the event handling function; 2, in the event handling function, the use of "element object .attr (" id "," changed id value ");" set the changed id value.

The operating environment of this tutorial: windows10 system, jquery3.2.1 version, Dell G3 computer.

How to change id with click in jquery

Bind the click event to the element

The click event occurs when an element is clicked.

The click () method triggers the click event, or specifies the function to run when the click event occurs.

Grammar

The click event that triggers the selected element:

(selector) .click ()

Add a function to the click event:

$(selector) .click (function)

Second, change the id value by attr method

The attr () method sets or returns the properties and values of the selected element.

When the method is used to return a property value, it returns the value of the first matching element.

When this method is used to set property values, one or more attribute / value pairs are set for matching elements.

Set properties and values:

$(selector) .attr (attribute,value)

Examples are as follows

(document) .ready (function () {$("button") .click (function () {$("div"). Attr ("id", "mydiv2");});}); # mydiv1 {background:#66ccff;} # mydiv2 {background:pink;}

Change the id attribute for the element

Output result:

Read this, the "jquery how to use click to change id" article has been introduced, want to master the knowledge of this article also need to practice and use to understand, if you want to know more about the article, welcome to follow the industry information channel.

Tags: Events elements attributes methods functions processes articles content objects appropriate multiple ideas tutorials new knowledge more steps versions environment computer knowledge Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information NVidia vpn Shulou Technology Huawei