Forum Moderators: open

Message Too Old, No Replies

How do i/in javascript "pass variable to relative link"

javascript and ajax

         

markcorrigan

12:18 pm on Aug 16, 2010 (gmt 0)

10+ Year Member



Hi can anyone help me.
I have a spin control that I obtained from this url. [switchonthecode.com...]

I want to take the output from this spin control and then place the output into a declared VARIABLE and then use it in a link similar to below.

<a href="#'VARIABLE HERE'">click here</a>

This, hyperlink, if clicked, will then allow the user to jump to this place in the current page. NB the # means it is a local / relative link that exists on the same page.

i.e. if 8 is selected on the spin control.
Then jump to
<a name="8">

Can anyone help me. I have tried to get this to run but seem flummoxed because I am not getting the variable to be able to work in the hyperlink.

Kind regards

Markjc@mweb.co.za

jason_m

2:05 pm on Aug 18, 2010 (gmt 0)

10+ Year Member



hey mark,
you are going to need to extract the 8 from the selection (i have not worked with spinners in a while so you may need to look this up), and add some sort of onclick (or whatever its called for spinners)

then use dhtml to do the following

document.getElementById('myID').setAttribute('name', myVariable);

markcorrigan

2:25 pm on Aug 18, 2010 (gmt 0)

10+ Year Member



Phew. thanks.
But I am not really that technically aware.
Can you be more specific.

The code that you gave me. How do I use this once I have extraced the 8. I am assuming that I must place it in a global variable (possibly an integer i.e. var int)
Then I need to make use of this through your code.
Once I have made use of it how is it going to engagte and call the html call to the #8

Thanks
Mark

Fotiman

2:43 pm on Aug 18, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



First, give your link an id. Change this:

<a href="#'VARIABLE HERE'">click here</a>

To this:

<a href="#" id="SpinControlLink">click here</a>

Next, you'll need to change your named anchors slightly because it's not valid for names to begin with a number. They must begin with a letter. So change this:

<a name="8">

To this:

<a name="S8">

Note, I've just added an 'S' (for SpinControl), but feel free to use whatever you like as long as it begins with a letter.

Next, assuming you've created an instance of SpinControl in variable spinCtrl, add the following bit of JavaScript:

spinCtrl.AttachValueChangedListener(function (sender, newVal) {
// sender is the SpinControl that triggered the event
// newVal is the new value
var el = document.getElementById('SpinControlLink');
el.href = '#S' + newVal;
});


So, when the value of spinCtrl changes, the anonymous function will be called which will find your link and update the href value to be '#S8' (or whatever value was selected). Note that I've used the same prefix character 'S' as I did with the named anchors. Make sure this matches whatever you're using.

markcorrigan

2:52 pm on Aug 18, 2010 (gmt 0)

10+ Year Member



Thanks you are a *
I really appreciate the help.
Mark