Home > Web Front-end > JS Tutorial > body text

How to change the ID of an element using JavaScript?

WBOY
Release: 2023-09-13 14:45:13
forward
1523 people have browsed it

如何使用 JavaScript 更改元素的 ID?

Generally speaking, the ID of any element in an HTML document is unique and can only be assigned to an element once. But the id can be changed later using JavaScript, following the same rules for uniqueness of the new ID, and only a single element can contain the new ID. In this article, we will look at ways to change the ID of an element using JavaScript.

JavaScript provides or allows us to use the id attribute to get the id of any element, as well as change or add a new ID to any HTML element in the document.

Let us learn with the help of a code example how to change the ID of an element in an HTML document through JavaScript using the id attribute.

grammar

The following syntax will explain you how to use the id attribute to change, get a new id, and add a new id to an HTML element -

Selected_element.id = newID;
Copy after login

In the above syntax, first we will use the HTML DOM to select the HTML element whose ID we want to change, and then assign a new id assignment operation to its right using the id attribute with the dot operator symbol.

Let us understand the actual implementation of id attribute changing the ID of an element in an HTML document through a code example.

algorithm

  • Step 1 - In the first step we will add an input element to get the new ID as input from the user and later we will change the ID of the same input element using the id attribute .

  • Step 2 - In this step, we will add a button element to the HTML document using the onclick event, which will later call a JavaScript function to change the ID of the input element. < /p>

  • Step 3 - In the next step, we will grab the input element and display the initial id of the element to the user so that they can easily see the changing ID of the element.

  • Step 4 - In the fourth step, we will define a JavaScript function where we will get the input element again and set its id equal to the text entered by the user in the input field And use the id attribute to change its id to this text.

  • Step 5 - In the final step we will use the id attribute to display the new ID assigned to the element and assign the function as a value to the onclick event so that it can be used later It is called when the user clicks the button.

Example

The following example will illustrate how to use the id attribute to change the id of an element in an HTML document -

<html>
<body>
   <h2>Changing ID of the element using JavaScript</h2>
   <p>Enter new ID, you want to give to element:</p>
   <input type = "text" id = "inp1"> <br> <br>
   <button id = "btn" onclick = "display()"> Click to change the ID </button>
   <p id = "result"> </p>
   <script>
      var result = document.getElementById("result");
      var inp1 = document.getElementById('inp1');
      result.innerHTML += " The ID of the input element before you entered ID of your choice is: <b> " + inp1.id + " </b> <br>"
      function display() {
         var inp = document.getElementById('inp1');
         var newID = inp.value;
         inp.id = newID;
         result.innerHTML += " The ID of input element after clicking the button is changed to: <b> " + inp.id + " </b> <br> ";
      }
   </script>
</body>
</html>
Copy after login

In the above example, we changed the ID of the input element using JavaScript's id attribute and displayed the old and new ID assigned to the input element on the user's screen.

Let's look at another code example where we will directly change the ID of the element within the onclick event rather than passing a function to it.

algorithm

The algorithms in the above example and this example are almost similar. You just need to change the value passed in the button element's onclick event, as shown in the example below.

Example

The following example will explain to you the changes that must be made in the previous algorithm to make it change the id of the input element like the previous example -

<html>
<body>
   <h2>Change ID of the element using JavaScript</h2>
   <p>Click the button below to change the ID of the below input element:</p>
   <input type = "text" id = "inp1", value = "Input Element"> <br> <br>
   <button id = "btn" onclick = "document.getElementById('inp1').id = 'newID'; return false">    Click to change the ID
   </button>
   <p id = "result"> </p>
   <script>
      var result = document.getElementById("result");
      var inp1 = document.getElementById('inp1');
      result.innerHTML += " The ID of the input element before you clicked the button is: <b> " + inp1.id + " </b> <br><br>"
      result.innerHTML += "<b>Note: </b>To see the change in the ID of the input element open the developer tools and inspect the input element to see its ID after clicking the button. ";
   </script>
</body>
</html>
Copy after login

In the above example, we can change the ID of the input element, but in this case, we cannot replace the previous id with the id of our choice because we statically assign the new ID value to the element . p>

In this article we learned how to change the id of an element present in an HTML document using an id of your choice and a static id that we want to give to that element, this static id must be unique and only the document can contain that A single element of id.

The above is the detailed content of How to change the ID of an element using JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!