Jump to content

Use Variable in document. .... line


GregL83
 Share

Recommended Posts

I am very new with javascript. I need to have a function that has the variable passed in it and used to locate a document element by id through a variable...something like thisfunction test(var){if(document.body.all.var.checked == true){document.getElementById(var).innerHTML=var}}<input type="checkbox" onClick="test('var1')" /><div id="var1"></div><input type="checkbox" onClick="test('var2')" /><div id="var2"></div>...Please Help! Thank you!

Link to comment
Share on other sites

You're pretty close:

<input type="checkbox" onClick="test(this, 'var1')" /><div id="var1"></div><input type="checkbox" onClick="test(this, 'var2')" /><div id="var2"></div><script type="text/javascript">function test(checkbox, id){	if(checkbox.checked)	{		document.getElementById(id).innerHTML = id;	}	else	{		document.getElementById(id).innerHTML = "";	}}</script>

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...