Add and Destroy HTML Element with Javascript

Posted on 19 March 2009. Filed under: Programming |

Here are some script to add and destroy element (in this case is div) with javascript :

 

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”&gt;

<html xmlns=”http://www.w3.org/1999/xhtml”&gt;

<head>

<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />

<title>Untitled Document</title>

<script>

var no = 0;

function addIjin(kolom, lapis) {

  var ni = document.getElementById(lapis);

 

no = no+1;

if (document.getElementById(kolom).value==”) {

alert(‘Masih kosong tuh’);

document.getElementById(kolom).focus();

return false;

}

var newdivIjin = document.createElement(‘div’);

   var divIdIjin = ‘Ijin’+no+’Div’;

   newdivIjin.setAttribute(‘id’,divIdIjin);

newdivIjin.innerHTML = no + “. ” + document.getElementById(kolom).value+”  <a href =\”#\” onclick=\”return delIjin(\’Ijin”+no+”Div\’, ‘”+no+”‘);\”>[-]</a>”;

ni.appendChild(newdivIjin);

 

document.getElementById(kolom).value = ”;

document.getElementById(kolom).focus();

}

function delIjin(nIjin, nElemen) {

var nocek = nElemen;

nocek = nocek*2/2+1;

  var nama_Div = document.getElementById(nIjin)

   obj = document.getElementById(nIjin);

   obj.parentNode.removeChild(obj);

   //alert (nocek);

   if (document.getElementById(“Ijin”+nocek+”Div”)) {

   //alert (no);

  for (var loop = 1; loop<=no – (nocek-1); loop++) {

   nama_Div.innerHTML= “tak rubah” + loop;

alert (loop + nama_Div);

 

  }

   } else {

   no = no – 1;

   }

}

</script>

</head>

 

<body>

<div id=”tambahan”>

<div id=”ijin”>

</div> 

</div>

 <p>

    <textarea name=”textarea” id=”textarea” cols=”45″ rows=”1″></textarea>

    <input type=”submit” name=”button” id=”button” value=”tambah” onclick=”return addIjin(‘textarea’, ‘ijin’);” />

</p>

 

</body>

</html>
 

Make a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

  • Siapa sih ini ?

  • Kategori

  • Masa Lalu

  • Follow me…

  • Apaan nih ?

  • Yang tersesat

    • 99,128 orang
  • Paling menyesatkan

    • Almanak

      March 2009
      M T W T F S S
      « Feb   May »
       1
      2345678
      9101112131415
      16171819202122
      23242526272829
      3031  

    Liked it here?
    Why not try sites on the blogroll...

    %d bloggers like this: