This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes HTML, CSS and JavaScript and the fly likes Pre select a dropdown using cookie. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Pre select a dropdown using cookie." Watch "Pre select a dropdown using cookie." New topic
Author

Pre select a dropdown using cookie.

forums UseR
Ranch Hand

Joined: Feb 24, 2009
Posts: 169
Hello there!

I am trying to use cookie to compare to a dropdown and preselect the dropdown upon a match.

Here is what I am trying so far...


This doesn't even persist the dropdown selection in the same session. I don't see the pop-ups (alert and prompt) neither. And same when I log back in, it won't show my previous selection (in my last session) .

What is the right way to achieve this!

thanks a lot!
Trilochan Bharadwaj
Ranch Hand

Joined: Feb 02, 2009
Posts: 100
where's your preselect thing getting called? is it using: ?
forums UseR
Ranch Hand

Joined: Feb 24, 2009
Posts: 169
Trilochan Bharadwaj wrote:where's your preselect thing getting called? is it using: ?


Hi Trilochan,

I re-examined my code, and made changes. You are right. I am using onload, here is my code:



I get error on line 96,


line 60,


I am not able to get the "dropdown option" selected by the user and also "all the items in the dropdown"
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
You are never calling the code to set the cookie value.

Eric
forums UseR
Ranch Hand

Joined: Feb 24, 2009
Posts: 169
Eric Pascarello wrote:You are never calling the code to set the cookie value.

Eric


Hi,
I made the corrections. Now I am getting undefined value (value for g in the code below), when I try to get the user selection from the dropdown. I am setting value of g in line 28.


I also get null error while getting the dropdown list (line 29, dl) to loop through and compare the cookie value.

Thanks!
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
not sure why you would be using this there. This scope would be the window and not the form.

document.formName.elementName
or
document.getElementById("elementId")

would be the correct way to access a form element.

Eric
forums UseR
Ranch Hand

Joined: Feb 24, 2009
Posts: 169
Eric Pascarello wrote:not sure why you would be using this there. This scope would be the window and not the form.

document.formName.elementName
or
document.getElementById("elementId")

would be the correct way to access a form element.

Eric


I followed your code and had it working. But the code is only getting executed when the screen gets loaded (all my code is in window.onload). The form has a button (along with dropdown). In the onSubmit I again have to call setCookie with the new user selection (in dropdown).

How to call javascript inside form element ?

Thanks a lot!
Trilochan Bharadwaj
Ranch Hand

Joined: Feb 02, 2009
Posts: 100

I'd use attachEventListener personally.

Trilochan.
forums UseR
Ranch Hand

Joined: Feb 24, 2009
Posts: 169
Trilochan Bharadwaj wrote:
I'd use attachEventListener personally.

Trilochan.


I didn't get your point. My form code is like this.


The code for button (go) is on line 9. How to relate function "setCookie" with the button. I have to associate 2 things with the onSubmit, one is to call an application which I am doing in the line 3, the second one is to call a javascript function "setCookie". Is it possible to associate two actions with onSubmit.

Thanks
Trilochan Bharadwaj
Ranch Hand

Joined: Feb 02, 2009
Posts: 100

Trilochan Bharadwaj
Ranch Hand

Joined: Feb 02, 2009
Posts: 100
ah i c ... so do you want those things to be invoked in order?

well this is off the bat, but you'd do this:


Hope this helps!
Trilochan
forums UseR
Ranch Hand

Joined: Feb 24, 2009
Posts: 169
I tried both options. Somehow, I am not able to associate setCookie with "onSubmit" button of the form. Is this because, I am going to a different page upon onSubmit click ?

As a result, the cookie is never set. Can you give me sample code as to how to write a javascript function inside form element.

Trilochan Bharadwaj
Ranch Hand

Joined: Feb 02, 2009
Posts: 100
Well you have onsubmit connected to something that is being called on server side, for the form, use onclick for the button and not onsubmit.
Trilochan Bharadwaj
Ranch Hand

Joined: Feb 02, 2009
Posts: 100
paste the code you're trying at the moment, I'll sift through it once I get out of the office ... I hope you're not in a rush.

Trilochan.
forums UseR
Ranch Hand

Joined: Feb 24, 2009
Posts: 169
I got it, I referred this link

Thanks a lot for your patience!
Resolved!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Pre select a dropdown using cookie.
 
Similar Threads
Passing array values using Ajax & JSP
Cookie Path does not set properly
RememberMe on login page
Loop through cookies and get value for a particular cookie name.
How to retain the value of drop down on click of browser back button