Solved

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

Posted on 2009-05-05
9
386 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JavaScript Loan Calculator Error 3 35
Use "if not" in a condition 2 19
Html using "Or" in condition 3 29
aria difference 2 15
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

730 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