Adding and Removing Attributes of Elements in Iframe

Adding and Removing Attributes of Elements in Iframe

I am trying to give elements on a page inside of an iframe a temporary ID if they don't already have one when they are clicked. I was able to add a temporary ID but I cannot remove it and give it to another element when it is clicked. The following are sample markup and the code I have used to give each element a temporary ID if it didn't already have one when clicked.

  1. <div>
  2.        <p> Sample1</p>
  3.        <div> Sample2 </div>
  4.        <span> Sample3 </span>
  5. </div>
  6. var myTarget ;
  7. var targetID ;
  8. var modifiedElement ;
  9. $('#myiframe').load(function () {
  10.     $(this).contents().find("body").on('click', function (event) {
  11.                                myTarget = $(event.target);                           
  12.                                targetID = myTarget.attr("id");                                

  13.                          if (modifiedElement == "modElement") {                             
  14.                              $(modifiedElement).removeAttr("contenteditable");
  15.                              $(modifiedElement).removeAttr("id");                             
  16.                          }                                  

  17.                          //Check if clicked element has the contenteditable attribute
  18.                          if (myTarget.attr('contenteditable') == undefined) {
  19.                              myTarget.attr('contenteditable', 'true');
  20.                             
  21.                             //Check if clicked element has an id
  22.                              if (targetID  == undefined) {
  23.                                  myTarget.attr("id", "modElement");
  24.                                  modifiedElement = "modElement";                              
  25.                              }
  26.                              else {
  27.                                  modifiedElement = targetID;                                 
  28.                              }                             
  29.                          };       
  30.                      })
  31. })
Please point out why the method above is not working. Thanks in advance.