INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Pre-check checkboxes via Post

Pre-check checkboxes via Post

(OP)
This is a pretty simple request, but I'm new to Javascript so thanks upfront for your patience.

I'm trying to pre-populate some check boxes based on values passed to a JSP page through either a POST or GET call.

So if my post string is:
http://www.example.com/form.jsp?animal=dog&color=blue

It would pre-check the "dog" and "blue" answer values my form.jsp page:

CODE

My favorite animal is:<br />
<input type="checkbox" name="animal" value="dog" />Dog<br />
<input type="checkbox" name="animal" value="cat" />Cat<br />
<input type="checkbox" name="animal" value="horse" />Horse<br />

My favorite color is:<br />
<input type="checkbox" name="color" value="blue" />Blue<br />
<input type="checkbox" name="color" value="red" />Red<br />
<input type="checkbox" name="color" value="green" />Green<br />

Obviously, my real-world application is more complicated than this; but I can figure it out based on the answer to this example.  

RE: Pre-check checkboxes via Post

This forum is intended for JSP server-side questions, for pure Javascript, you should use this one

Anyway, the checked attribute should do the trick.

CODE

My favorite animal is:<br />
<input type="checkbox" name="animal" value="dog" checked/>Dog<br />
<input type="checkbox" name="animal" value="cat" />Cat<br />
<input type="checkbox" name="animal" value="horse" />Horse<br />

My favorite color is:<br />
<input type="checkbox" name="color" value="blue" checked />Blue<br />
<input type="checkbox" name="color" value="red" />Red<br />
<input type="checkbox" name="color" value="green" />Green<br />

Cheers,
Dian

RE: Pre-check checkboxes via Post

(OP)
Thanks Diancecht,

I used the checked attribute, but had to modify the code a bit.

Here's what ended up working:

CODE

<input type="checkbox" name="animal" value="dog" ${param.animal == 'dog' ? 'checked' : ''}/>Dog

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close