Link to home
Start Free TrialLog in
Avatar of BearWebContent
BearWebContentFlag for Bosnia and Herzegovina

asked on

Javascript to write cookie for state selector used on another page

This is the information that I received, but being a novice do not know what code to add to the attached javascript file or the page where the state is selected to make it work. Can anyone help me? The idea is that the person selects a state on one page that takes them to another URL.  On that page, the user clicks another link that takes them to a page with a pre-selected state in the result based on the cookie initially selected.

Refer to: http://jason.pureconcepts.net/articles/javascript_cookie_object
- In the function setDDValue, a cookie can be set in the closure between line 117 and 124
- This cookie would have the same value as the variable 'value', passed in the function (i.e.: 'value' would be something like "Alaska")
- Since the Prototype library cannot "click" things programmatically, an event listener for a custom event needs to be created in order for it to work. Copy line 93, and paste it immediately after, and change it with a new custom even name, eg.:

Event.observe(option, 'stateSelected', (observeOption), false);

On the page that needs to be smart enough to auto select the city:

- Read the cookie stored
- If cookie exists, fire the stateSelected event for the corresponding state link, ie: $('rrtaa67').fire('stateSelected'); where rrtaa67 is the ID name of the link in custom dropdown.
- This must be after the page has done loading, so bind it to the document load event.
dropdown.js
Avatar of SRigney
SRigney
Flag of United States of America image

You can probably use the Animate method from jQuery and the Comlete method. Once the animation is complete you could inject the link into the div at that point so the slide happens completely before the text appears.

http://api.jquery.com/animate
ASKER CERTIFIED SOLUTION
Avatar of Pravin Asar
Pravin Asar
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry, wrong window.

I don't think you have to go through the code to click the state dropdown.  You should be able to set the selected index to the value that matches the state stored in the cookie without needing to click on it.

Based on the info you have though.
On both the page you want to save the cookie, and the page you want to read the cookie you need to include the script from http://jason.pureconcepts.net/articles/javascript_cookie_object

At line 119 I think you need to add
                  Cookie.init({name: 'mydata'}, {state: Input.value, x: 0});


Then on the page that you want to set the value add the following javascript
document.observe("dom:loaded", function() {
$("selectboxTopreSet").value(  Cookie.getData('state') );
 $('selectboxTopreSet').fire('stateSelected');
});
Avatar of BearWebContent

ASKER

Scenario:
I set the cookie using the form code below.  Selecting a state from the drop-down takes me to xyz.html (or wherever else).  On xyz.html is a link that opens a shadow box with another state selector that should be automatically open to the right state information. (See state-selector-xyz.html)

Is this clearer?  

 state-selector-xyz.html state-selector-xyz.html
<form>
			<div id="compare-select" class="select hide-dropdown">
				<div class="wrapper">
					<a onClick="return observeOpener(this);" href="" class="dd-opener">Select a state</a>
					<a onClick="return observeOpener(this);" href="" class="dd-opener pulldown-arrow"></a>
				</div>
<ul class="selectbox"><li><a href="??" value="AL">Alabama</a></li><li><a href="??" value="AK">Alaska</a></li><li><a href="??" value="AZ">Arizona</a></li><li><a href="??" value="AR">Arkansas</a></li><li><a href="??" value="CA">California</a></li><li><a href="??" value="CO">Colorado</a></li><li><a href="??" value="CT">Connecticut</a></li><li><a href="??" value="DE">Delaware</a></li><li><a href="??" value="FL">Florida</a></li><li><a href="??" value="GA">Georgia</a></li><li><a href="??" value="HI">Hawaii</a></li><li><a href="??" value="ID">Idaho</a></li><li><a href="??" value="IL">Illinois</a></li><li><a href="??" value="IN">Indiana</a></li><li><a href="??" value="IA">Iowa</a></li><li><a href="??" value="KS">Kansas</a></li><li><a href="??" value="KY">Kentucky</a></li><li><a href="??" value="LA">Louisiana</a></li><li><a href="??" value="ME">Maine</a></li><li><a href="??" value="MD">Maryland</a></li><li><a href="??" value="MA">Massachusetts</a></li><li><a href="??" value="MI">Michigan</a></li><li><a href="??" value="MN">Minnesota</a></li><li><a href="??" value="MS">Mississippi</a></li><li><a href="??" value="MO">Missouri</a></li><li><a href="??" value="MT">Montana</a></li><li><a href="??" value="NE">Nebraska</a></li><li><a href="??" value="NV">Nevada</a></li><li><a href="??" value="NH">New Hampshire</a></li><li><a href="??" value="NJ">New Jersey</a></li><li><a href="??" value="NM">New Mexico</a></li><li><a href="??" value="NY">New York</a></li><li><a href="??" value="NC">North Carolina</a></li><li><a href="??" value="ND">North Dakota</a></li><li><a href="??" value="OH">Ohio</a></li><li><a href="??" value="OK">Oklahoma</a></li><li><a href="??" value="OR">Oregon</a></li><li><a href="??" value="PA">Pennsylvania</a></li><li><a href="??" value="RI">Rhode Island</a></li><li><a href="??" value="SC">South Carolina</a></li><li><a href="??" value="SD">South Dakota</a></li><li><a href="??" value="TN">Tennessee</a></li><li><a href="??" value="TX">Texas</a></li><li><a href="??" value="UT">Utah</a></li><li><a href="??" value="VT">Vermont</a></li><li><a href="??" value="VA">Virginia</a></li><li><a href="??" value="WA">Washington</a></li><li><a href="??" value="WV">West Virginia</a></li><li><a href="??" value="WI">Wisconsin</a></li><li><a href="??" value="WY">Wyoming</a></li></ul>

<input name="compare-savings" value="" type="hidden"></div>
</form>

Open in new window