Solved

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

Posted on 2009-05-05
9
382 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

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 article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

821 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