Jump to content

newbie needs help on javascript


gencom97

Recommended Posts

Hello. I hope I'm not asking a stupid question, but I'm new to javascript, so I would appreciate any advice. Here I'm writing a function that is called when a pull-down menu's option from an ElementById("kab") is changed (onChange). This function creates an ElementById ("kec") element at an ElementById ("kecamatan"). The problem is, when the user selects different options from the element ("kab"), it always create a new element ("kec") despite ("kec") already exists. How can I delete the previous element ("kec")? Can anyone help me? Thank you

 

function kabkec (form) 

{	

    	x = form.kab.selectedIndex;

y = form.kab.options[x].value;

a=document.getElementById("kecamatan");

b=document.createElement("select");

b.setAttribute("id","kec");

b.setAttribute("name","kec");

b.setAttribute("class","isi");

if (x!=0)

{

	c=document.createElement("option");

	d=document.createTextNode("Seluruh " + y);

	c.setAttribute("value","");

	c.appendChild(d);

	b.appendChild(c);

	a.appendChild(b);

}

switch (x)

{

	case 1:

	{

		Kodya = ["Danurejan","Gedongtengen","Gondokusuman","Gondomanan","Jetis","Kotagede","Kraton","Mantrijeron","Mergangsan","Ngampilan","Pakualaman","Tegalrejo","Umbulharjo","Wirobrajan"];

		for (i=0;i<14;i++)

		{

			c=document.createElement("option");

			d=document.createTextNode(Kodya[i]);

			c.setAttribute("value",Kodya[i]);

			c.appendChild(d);

			b.appendChild(c);

			a.appendChild(b);

		}

		break;

	}

	case 2:

	{

		Sleman = ["Berbah","Cangkringan","Depok","Gamping","Godean","Kalasan","Minggir","Mlati","Moyudan","Ngaglik","Ngemplak","Pakem","Prambanan","Seyegan","Sleman","Tempel","Turi"];

		for (i=0;i<17;i++)

		{

			c=document.createElement("option");

			d=document.createTextNode(Sleman[i]);

			c.setAttribute("value",Sleman[i]);

			c.appendChild(d);

			b.appendChild(c);

			a.appendChild(b);

		}

		break;

	}

	case 3:

	{

		Bantul = ["Bambanglipuro","Banguntapan","Bantul","Dlingo","Imogiri","Jetis","Kasihan","Kretek","Pajangan","Pandak","Piyungan","Pleret","Pundong","Sanden","Sedayu","Sewon","Srandakan"];

		for (i=0;i<17;i++)

		{

			c=document.createElement("option");

			d=document.createTextNode(Bantul[i]);

			c.setAttribute("value",Bantul[i]);

			c.appendChild(d);

			b.appendChild(c);

			a.appendChild(b);

		}

		break;

	}

	case 4:

	{

		Kulonprogo = ["Galur","Girimulyo","Kalibawang","Kokap","Lendah","Nanggulan","Panjatan","Pengasih","Samigaluh","Sentolo","Temon","Wates"];

		for (i=0;i<12;i++)

		{

			c=document.createElement("option");

			d=document.createTextNode(Kulonprogo[i]);

			c.setAttribute("value",Kulonprogo[i]);

			c.appendChild(d);

			b.appendChild(c);

			a.appendChild(b);

		}

		break;

	}

	case 5:

	{

		Gunungkidul = ["Gedangsari","Girisubo","Karangmojo","Ngawen","Nglipar","Paliyan","Panggang","Patuk","Playen","Ponjong","Purwosari","Rongkop","Saptosari","Semanu","Semin","Tanjungsari","Tepus","Wonosari"];

		for (i=0;i<18;i++)

		{

			c=document.createElement("option");

			d=document.createTextNode(Gunungkidul[i]);

			c.setAttribute("value",Gunungkidul[i]);

			c.appendChild(d);

			b.appendChild(c);

			a.appendChild(b);

		}

		break;

	}

}

return true;

}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.