0

I have been searching for an answer for this for days and haven't found anything so sorry if this has been asked before.

I want the integer to go up by 1 every time this link is clicked. So first it is 0, then clicked its 1, then clicked its 2, then clicked its 3

document.getElementById('myPackage').href="http://www.vanquishvps.com/order/cart.php?a=confproduct&i="+1

I really have no idea how to do this, I have really tried looking at Jquerys variables and stuff, I just cant figure it out.

Id appreciate any help hugely!

Thank you

1
  • 1
    When the link is clicked does the page reload? It looks like you have to handle the increment on the server side in PHP. Commented May 8, 2011 at 13:59

4 Answers 4

1

After looking at the site that you mentioned in your comment. This should work in your specific scenario:

jQuery('#myPackage').click(function () {
  // Get the first 3 parts of link's href
  var hrefParts = jQuery('#myPackage')[0].href.split('=').slice(0,2);

  // Add the current slots value
  hrefParts.push(jQuery('.slots span').text())

  // Set the href to the new URL
  this.href = hrefParts.join('=');
});

Another solution would be to modify your existing setPackageData function and add these lines to the end of it. This solution would set the link to the package index, instead of the number of slots, and also does it whenever you change the slider, not just when you click the link:

var myPackage = $('#myPackage')[0], hrefParts = myPackage.href.split('=').slice(0,2);
hrefParts.push(index.toString());
myPackage.href = hrefParts.join('=');

Although you should have your developer look at it, she or he will probably come up with a more permanent solution ;)

Sign up to request clarification or add additional context in comments.

10 Comments

Thanks for your help! I will definitely get him to finish things when he's back but I need this up and running tomorrow really for some clients. I have put this in my slotSlider.js file but no luck. Am I missing something? art-williams.com/craft
Seems to work for me in Firefox 4. Although you still have Avitus' solution in there and it's causing a JavaScript error. What browser are you testing in? What makes you think it's not working?
Oh brilliant, I see it works when clicked. Fantastic, I will get rid of Avitus' solution I forgot about that! One thing, you have linked it fantastically but it is adding to the amount of slots, is there a way to have it add to the package. So the first package is 0, the second package is 1, the third is 2 etc? I don't mean to sound rude I appreciate all this help so much
Ah, I see. If you don't mind modifying the existing code, I've edited my answer with a different solution.
Wow thank you for your help, so am I just completely replacing the previous edit with this new paragraph?
|
0

You need to attach a click event handler:

Vanilla Javascript:

 document.getElementById('myPackage').attachEvent('click', function()
 {
      // If it's some script value;
      someValue++;

      // If it's the text of your link...
      var currentValue = parseInt(this.value);
      this.value = currentValue++;

      return false;
 });

jQuery:

$('#myPackage').click(function()
{
     someValue++;
     $(this).val(parseInt(this.value)++);
});

2 Comments

This looks great, I appreciate all the help, but how do I attach a click handler? I am definitely the simpliest of novices at this :/
The code provided is the act of attaching a click event handler.
0

You need to have a global variable in javascript to have the id and then bind the click event.

var myID = 0;

$("#myPackage").click(function() {
      myID++;
      window.location = "http://www.vanquishvps.com/order/cart.php?a=confproduct&i=" + myID;
});

3 Comments

I really am very embarressed but how do I assign a global ID and bind the click event?
if you just paste the code I have put into a script tag in your page and make sure that you've included the jquery library this code should just work. you'll probably have to include a target tag in your anchor tag also so that you maintain this window
I have put it in the code, basically my friend made me this site and has now left to go on holiday, so i'm stuck. But here it is art-williams.com/craft . The order now link is still at 0, as I want as the slider goes up it increases by 1 for the href.
0

store your number variable somewhere else.

var index = 0;

and then use it to change your link's url

function changeUrl() {

index++;

document.getElementById('myPackage').href="http://www.vanquishvps.com/order/cart.php?a=confproduct&i=" + index;

}

so, when the user clicks wherever you want him to click to change the url, you just have to call the changeUrl function

document.getElementById('elementThatWillChangeTheUrl').click = changeUrl;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.