Solved

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

Posted on 2009-05-05
9
383 Views
Last Modified: 2012-05-06
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

0
Comment
Question by:bmwlaval
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 19

Expert Comment

by:erikTsomik
ID: 24307556
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>
0
 
LVL 1

Author Comment

by:bmwlaval
ID: 24307573
No unfortunately, that did not change anything, I even tried removing the target="" alltogether and it still didn't help...
0
 
LVL 12

Expert Comment

by:alien109
ID: 24307582
<a href="#" onclick=":seturl();return false">Link</a>
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 12

Expert Comment

by:alien109
ID: 24307587
oops. left a colon in there.

<a href="#" onclick="seturl();return false">Link</a>
0
 
LVL 19

Accepted Solution

by:
erikTsomik earned 70 total points
ID: 24307645
try this
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>User Calendar</title>
<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";
    }  
            return false;
      
}
</script>
</head>

<body>
  <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="javascript:seturl();" target="_self">Link</a>
</form>
</body>
</html>
0
 
LVL 1

Author Comment

by:bmwlaval
ID: 24307733
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?
0
 
LVL 4

Assisted Solution

by:jwmcpeak
jwmcpeak earned 55 total points
ID: 24308418

<a href="#" onclick="return seturl();">Link</a>
 
// new seturl() function
function seturl()
{
    var value = document.getElementById('model').value;
    var url;
    if (value == '1')
    {
       url="http://mywebsite.com/page1.php";                  
    }
    else if(value == '2')
    {
       url="http://mywebsite.com/page2.php";
    }
 
    window.open(url);
    return false;                   
}

Open in new window

0
 
LVL 1

Author Closing Comment

by:bmwlaval
ID: 31578143
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!
0
 
LVL 19

Expert Comment

by:erikTsomik
ID: 24308517
thank you
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Someone recently asked me about how to display a progress indicator on a page while an iframe is loading. And I remember when I first came across this myself. It was a bit tricky to get my head around, but really, it's very simple. The most impor…
This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

837 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question