Link to home
Start Free TrialLog in
Avatar of bmwlaval
bmwlaval

asked on

using onClick() in the <a> tag with href='#'

I am trying to use a variable set inside my page that will change the address of the next page.
For ex: page1 has a radio group, lets just say 1,2,3,4. If I choose 1 I go to page2 and so forth.

I have the code for the radio group and for the actual link, my problem is creating the javascript to work with the <a> tag to get there. Everything that I have read about says that I have to use href="#" but when I use this, it opens a new window and its always the same as the page you were just on...

Does anyone know the proper syntax for doing this?

Here is some of my code.
<form>
<input type="radio" id="model" name="model" value="1" />1<br />
<input type="radio" id="model" name="model" value="2" />2<br />
 
<a href="#" onClick="javascript:seturl();" target="_blank">Link</a>
</form>
 
//and here's the java script
 
<script type="text/javascript" language="javascript">
function seturl()
{
   var value = document.getElementById('model').value;
   if (value == '1')
   {
      document.location.href="http://mywebsite.com/page1.php";			
   }
   else if(value == '2')
   {
      document.location.href="http://mywebsite.com/page2.php";
    }				  	
}
</script>

Open in new window

Avatar of erikTsomik
erikTsomik
Flag of United States of America image

try changing the target
from <a href="#" onClick="javascript:seturl();" target="_blank">Link</a>

to this
<a href="#" onClick="javascript:seturl();" target="_self">Link</a>
Avatar of bmwlaval
bmwlaval

ASKER

No unfortunately, that did not change anything, I even tried removing the target="" alltogether and it still didn't help...
<a href="#" onclick=":seturl();return false">Link</a>
oops. left a colon in there.

<a href="#" onclick="seturl();return false">Link</a>
ASKER CERTIFIED SOLUTION
Avatar of erikTsomik
erikTsomik
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
So erik, that worked, but why doesnt it work when target="_blank", cause thats what I really want it to do. I want these links to be opening in outside pages...is there any other solution?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Seeing erik answered my main question i gave him a few more points, hope thats ok with you guys....thank you so much for this, I was really stuck and it was driving me nuts!
thank you