2 Replies - 1681 Views - Last Post: 15 March 2009 - 04:27 AM

#1 REHANAZHER  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 14-March 09

Accessing A javascript variable in HTML

Post icon  Posted 14 March 2009 - 11:18 PM

Hi All,

I have two Select Boxes on my web page, 2nd being populated based on selection of first select box.

Now I am suppose to get the selected value of 2nd Select and put it in a link i.e.
<a href='abc.html?id=<2nd select box value> 


I can get the selected value using Javascript with the following code:
2ndid = form.2ndodf.options[form.2ndodf.options.selectedIndex].value; 



where 2ndid is a global javascript variable.


How I can use this variable into <a href> tag.

2nd idea may be to use PhP but I am unable to do so.

Any suggestions please.

Thanks and regards,

Is This A Good Question/Topic? 0
  • +

Replies To: Accessing A javascript variable in HTML

#2 JohnWaters  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 138
  • Joined: 28-November 08

Re: Accessing A javascript variable in HTML

Posted 15 March 2009 - 04:22 AM

Hi, Im not 100% sure what your asking.

Maybe this bit of code might help you ...

<html>
	<head>
		<script type="text/javascript">
			var myURL ="abc.html";
			function updateURL(value){
				document.getElementById("myLink").href = myURL+"?id="+value
			}
		</script>
	</head>
	<body>
		<select id="mySelect" onchange="updateURL(this.value)">
			<option value="AA" selected>AA</option>
			<option value="BB">BB</option>
			<option value="CC">CC</option>
		</select>
		<p>
		<script type="text/javascript">
			document.write('<a id="myLink" href="'+myURL+'?id='+document.getElementById("mySelect").value+'">MY URL</a>');
		</script>
		</p>
	</body>
</html>



All the best :)
Was This Post Helpful? 0
  • +
  • -

#3 mocker  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 50
  • View blog
  • Posts: 466
  • Joined: 14-October 07

Re: Accessing A javascript variable in HTML

Posted 15 March 2009 - 04:27 AM

You'll want the select box to change the <a> value whenever it changes.. which means you'd put the javascript into its onchange event.

First you need to give the <a> tag an id so you can refer to it easily
<a id='aSelectLink' href='abc.html'>blah blah</a>



Then add the code to update it to the select object
<select .....	onchange="document.getElementById('aSelectLink').href = 'abc.html?id='+ this.options[this.selectedIndex].value; " >


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1