- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to set new id attribute with jQuery?
To implement this, extract id from attr() and use replace() to replace the id attribute.
Example
Following is the code −
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"> <script src="https://code.jquery.com/jquery-1.12.4.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> <body> <div id="first-name"> </body> <script> $('[id*="-"]').each(function () { console.log('Previous Id attribute: ' + $(this).attr('id')); $(this).attr('id', $(this).attr('id').replace('-', '----')); console.log('Now Id Attribute: ' + $(this).attr('id')); }); </script> </html>
To run the above program, save the file name “anyName.html(index.html)”. Right click on the file and select the option “Open with Live Server” in VS Code editor.
Output
This will produce the following output −
Following is the value on console −
- Related Articles
- How to set new value for an attribute using jQuery?
- How to get the value of id attribute in jQuery?
- How to get objects by ID, Class, Tag, and Attribute in jQuery?
- How to create a new img tag with JQuery, with the src and id from a JavaScript object?
- How can I select an element by its ID attribute using jQuery?
- HTML id Attribute
- How to change the element id using jQuery?
- jQuery #id Selector
- How to remove disabled attribute using jQuery?
- How to select element with specific class and title attribute using jQuery?
- How to combine a class selector and an attribute selector with jQuery?
- How to set “checked” for a checkbox with jQuery?
- How to write a jQuery selector to find links with # in href attribute?
- How do we use jQuery Attribute selector with a period?
- How to get an attribute value in jQuery?

Advertisements