Solved

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

Posted on 2009-05-05
9
376 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
 
LVL 12

Expert Comment

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

<a href="#" onclick="seturl();return false">Link</a>
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Joomla main HTML page 4 47
Alert not firing 2 34
Paper tearing animation 6 43
Link SQL table to Webpage 9 39
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…
Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now