PDA

View Full Version : Accessing JavaScript Variables from HTML



GIS Don
May 6th, 2005, 07:21 PM
Hi All,

Is there a way to use a variable created in JavaScript as part of a HTML.

For example:

<script language=JavaScript type=text/javascript>
var linkto = "";
if (frmForm.chkHere.checked) linkto = "Here";
if (frmForm.chkThere.checked) linkto = "There";
if (frmForm.chkEverywhere.checked) linkto = "Everywhere";
else linkto = "SomeplaceElse";
</script>

....then later in the same HTML Document.....

<a href=linkto+".com"><Click This For Link></A>

I need to find a way to forward someone to a different page based on which checkbox they check off. There must be some way to do this, but for some reason I just can't come up with anything.

Any help out there?

Thanks,
Don

SpirtOfGrandeur
May 6th, 2005, 07:44 PM
First you must give your link an ID:


<a href='#' id='myLink'><Click This For Link></A>

Then when you run the script code:

<script language=JavaScript type=text/javascript>
var linkto = "";
if (frmForm.chkHere.checked) linkto = "Here";
if (frmForm.chkThere.checked) linkto = "There";
if (frmForm.chkEverywhere.checked) linkto = "Everywhere";
else linkto = "SomeplaceElse";
document.getElementById('myLink').href = linkto + '.com';
</script>

warhammerdude20
May 6th, 2005, 08:00 PM
or



<body>

<script>
document.writeln("<a href='"+linkto+".com'>click this for a link</a>");
</script>

</body>

GIS Don
May 10th, 2005, 07:46 PM
Thanks SpirtOfGrandeur and warhammerdude20. Both suggestions worked. Not sure if my new problem is related or not.

My original post was a little too simplified. I'm actually having more than one check box link to the same link in some cases. But no matter what is checked, it always evaluates to the last option for the link.

Here's what I mean.

<a href='#' id='myLink'><Click This For Link></A>

<script language=JavaScript type=text/javascript>
var linkto = "";
if (frmForm.chkHere.checked || frmForm.chkHereToo.checked) linkto = "Here";
if (frmForm.chkThere.checked) linkto = "There";
if (frmForm.chkEverywhere.checked || frmForm.chkNoWhere.checked) linkto = "Everywhere";
else linkto = "SomeplaceElse";
document.getElementById('myLink').href = linkto + '.com';
</script>

The link produced always leads to "SomeplaceElse.com", no matter what is checked. The link never leads to Here or There or Everywhere, just SomeplaceElse.

I have tried both methods suggested and I get the same results. The form hasn't been submitted when the user is to click on the link. So I'm not sure if that could be it. I have inserted an Alert message displaying the value of the link, and it shows the correct link name.

I think my syntax for the conditional statements is correct.

Any help?

Thanks,
Don

GIS Don
May 10th, 2005, 08:06 PM
Hi All,

Just a note to say please ingore my last post about still having problems. I got the conditional Statements to work properly by placing it all in a function and calling the funtion from the checkbox using OnClick.

Thanks for all the help.
Don

albright_family
Nov 7th, 2006, 01:34 PM
Ok I think I am following what you guys were saying, but for whatever reason, it isn't working for me. BTW I am using this in a ASP page.

Here is what I have:
<head>
<SCRIPT TYPE="text/javascript">
<!--
function ATICheck(a){
var checkati = confirm ("This item was not found in your inventory. Would you like to add it now?");
if (checkati)
return "yes"
else
return "no"
}

//-->
</SCRIPT>
</head>


and down the page a ways, I have
set addtoinventory = document.write(ATIcheck())


So basically what I am trying to do is this:
Based on the condition of an SQL query (that works), if there are no records (this portion of the code works), then ask if you want to add a record (THIS IS WHERE MY PROBLEM IS), and then add that record based on the javascript confirm results.

Any help would be greatly appreciated.

Thank You
Russell