![]() |
Stuck on a little Javascript project, could use a little help.
Hey,
So I have been working on a little piece of Javascript and I have gotten a little stuck. When I say that I have honestly tried for a couple hours but I can't seem to get anywhere with it... I am writing a little selector script to allow a user to select multiple options and then create a url dependent on the options the user has selected. Code:
<div onclick="generate_url('data input by php')">A button</div>http://www.mywebsite.com/page.php?id...neselectiontwo (I can go ahead and change out the final url myself that's not the problem) The problem is to do with the count it automatically jumps through the script and adds the first selection twice then loads the page before you can select a second option. I attempted to move around the if statements however this had no effect. Could someone point me in the right direction |
I went and changed some of the script around again.
So that the if(count==1) is above if(count==0). What I now get is the url followed by: undefinedandthedatafromselection2 The data from the first selection is not brought into the third if statement and then appended to the url. |
Let's step through your code ... line by line. You can easily do that with pen & paper.
Please try to reproduce the steps 1 - Click single mode button ... so mode will be 'single' 2 - Oh, you initialize count with a string. Why? You should use this Code:
var count = 0;Code:
generate_url('data input by php')4 - Inside generate_url() the first if statement will be true (because mode=='single'), so you go on with this part of the method 5 - You increase count by 1 ... so count == 1 now 6 - Now you add the "selection" to the url 7 - if count == 1 (this is true) you change window.location.href which results in an immediate page refresh In 'dual' mode 4 - The first if will be false 5 - The second if will be true, because mode=='dual' 6 - count is still 0, so the first if will be true 7 - you copy "selection" to "selection_1" and increase count by 1 (so count == 1) 8 - the second if is true ... but IS STILL WRONG because you are using the assignment operator "=" instead of the comparison operator "==". This will set count to 1 and the result of the if condition is true 9 - you copy "selection" also to "selection_2" and increase count by 1 (so count == 2) 10 - The third if is also true because count==2, so you append both copies of "selection" to the url and immediately refresh the browser because you change window.location.href |
Okay
So I changed a couple things and this is what I have got: Code:
<div onclick="generate_url('data input by php')">A button</div>I also fixed the if(count==1) statement so it's not using an assignment. I moved the if(count==1) statement above the if(count==0) statement so it would only activate the second time around. Previously it would add the first user input twice into the url. I am trying to get it so on the first click of one button it adds the first value dependent on what button the user clicked and on the second click it then adds a second user input and creates a url. Which then javascript sends the user too. I now have the problem where it creates the following url mywebsite.com/whatever?id=undefined(SECOND INPUT) It should give the following mywebsite.com/whatever?id=(FIRST INPUT)(SECOND INPUT) I think it's to do with the variables not being passed from one loop to the next. I am really a Javascript newbie. If you could help me get this working I would much appreciate it. |
With the "var" keyword you are declaring "selection_1" and "selection_2" as local variables to the method. Those local variables will be reinitialized with each call of the method.
If you omit the "var" keyword it should work better. |
| All times are GMT +1. The time now is 02:36 PM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.