?
Solved

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

Posted on 2009-05-05
9
Medium Priority
?
400 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 280 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 220 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …
Suggested Courses

752 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