LearnFast.Ninja

jQuery change ID

create custom element, create javascript function, call it changeId. You can do this for all attributes such as: class, id etc.

Step 1: create custom element

<div id="oldId" onclick="changeId('oldId', 'newId')">

    jQuery change ID - old ID text

</div>


Step 2: create javascript function, call it changeId

function changeId(oldId, newId) {

    $("#" + oldId).attr("ID", newId);

    //only for debug

    $("#" + newId).html($("#" + newId).html() + '<br />' + 'New text');

}


The main idea:
  $("#" + oldId).attr("ID", newId);

Complete code:

<html>

<head>

    <title>jQuery change ID </title>

    <script type="text/javascript" src="http://code.jquery.com/jquery-1.4.4.min.js"></script>

    <script type="text/javascript" src="http://remysharp.com/downloads/jquery.marquee.js"></script>

    <script type="text/javascript">

        function changeId(oldId, newId) {

            $("#" + oldId).attr("ID", newId);

            //only for debug

            $("#" + newId).html($("#" + newId).html() + '<br />' + 'New text');

        }

    </script>

</head>

<body>

    <div id="oldId" onclick="changeId('oldId', 'newId')">

        jQuery change ID - old ID text

    </div>

</body>

</html>


Result:


Tags

jquery

jquery-rename-id jquery-rename-attribute jquery-rename-class