How to change id attribute with jquery after load page ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • oranoos3000
    New Member
    • Jan 2009
    • 107

    How to change id attribute with jquery after load page ?

    hi

    can i change id attribute an element with below command in jquery

    Code:
    $("#error").attr("id", "warning");

    i know this code is not reasonable but i want to know change id's element after load page is incorrect and hos is state of validate code after change id an element?

    id attribute is identifier for element. writing this code dont cause i lose this element in dom tree?


    thanks alot for your help
  • RamananKalirajan
    Contributor
    • Mar 2008
    • 608

    #2
    Hi,
    I am not good at jquery (i haven't worked much on that) but in normal javascript you can change the id attribute of an element after the page load. Please find the sample attached.

    Code:
    <html>
    <head>
    <script type="text/javascript">
    var count=1;
    function showId(){
    	var divObj = document.getElementsByTagName("div");
    	alert("Id of the div is "+divObj[0].id);
    }
    function changeId(){
    	var divObj = document.getElementsByTagName("div");
    	divObj[0].setAttribute("id","div"+count);
    	count++;
    }
    </script>
    </head>
    <body>
    <div id="div0">
    	This is the div element.
    </div>
    <input type="button" value="Change Id" onclick="changeId()" />
    <input type="button" value="Show Id" onclick="showId()" />
    </body>
    </html>
    Thanks and Regards
    Ramanan Kalirajan

    Comment

    • oranoos3000
      New Member
      • Jan 2009
      • 107

      #3
      hi

      is'nt id attribute a identifier for any element in the dom tree?
      my meaning is this "if i change id attribute after load page , can access to element that i changed it's id via dom tree"?

      Comment

      • RamananKalirajan
        Contributor
        • Mar 2008
        • 608

        #4
        Yes Id element is an identifier for an element in DOM tree. But DOM has provided methods to change the attribute of the elements. It's upto you to change the attribute or not. If you have the requirement to changed the id then you can. But there should not be a duplication. I am giving you a scenario were I changed the ID's.

        I had one table and given the id for tr as tr1, tr2 respectively. If the user deletes a tr in the mid of the table means I called a method to reSequence the Id so that I have id's in a sequence.

        Post back if you need any help.

        Thanks and Regards
        Ramanan Kalirajan

        Comment

        • oranoos3000
          New Member
          • Jan 2009
          • 107

          #5
          thank you RamananKaliraja n, i understand

          Comment

          Working...