Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

populating select when page loads

 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I have a form which contains a select. I need this select to be populated with values from database when page loads. I tried it using onload in body to call a javascript function populate() that fills the select, but it doesn't fill it (i guess document.getElementById() cannot find the form id because there isn't a body yet when calling onload, dunno, i'm not too skilled in javascript, maybe someone can clear this thing out for me). I created a button to test the javascript and java code and onclick does the job ok... only that i need the select populated when the user enters the page. Can someone give me some advice on how to implement this?

Thanks,
 
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You normally just use servlet / JSTL / EL. Just map a servlet on the desired URL, implement doGet() to load the list and set it in the request scope, use JSTL/EL in JSP to display the list.

If you really, really want to do this with Javascript for some reasons (Ajaxical stuff?), then you're here in fact at the wrong place. There's a HTML/Javascript forum out. Have patience and a moderator will move the topic sooner or later.
 
Nick Potter
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bauke Scholtz wrote: (Ajaxical stuff?)



Yep.
Sorry, a moderator please move it to the appropriate forum. Thanks
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you using a homegrown library based on some basic tutorials and zero knowledge about Javascript and the browser world, or using a robust, well developed and thoroughly tested 3rd party library like jQuery?

I would highly recommend to start with the latter. Save yourself from effort, trouble and pain.
 
Nick Potter
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Something in between. I had 0 knowledge about JavaScript when we started the project, so i'm not too comfortable with it. I also lack the time for now to read a book on it. Thanks for the recommendation, will keep that in mind.

Btw, can you recommend a book on JQuery? To be honest, it's the first time i heard this term.
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
jQuery is a Javascript library written by someone who already went through all that effort and pain for you to create a robust and crossbrowser compatible ajaxical library. There are alternatives as well, like Prototype, Moo, Scriptaculous, Dojo and so on. I just mentioned jQuery because of having the best experience with it.

A book is 'jQuery in Action' written by under each Bear Bibeault who walks around at those forums as well.

It has a website as well: http://www.jquery.com -how straightforward.
 
Sheriff
Posts: 67592
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bear walking!

Moved to the HTML/JavaScript forum.
 
Bear Bibeault
Sheriff
Posts: 67592
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I second Bauke's recommendation. Most experienced Ajax developers will advise you to adopt a library rather than try to write "raw" Ajax code. There are browser differences and subtleties that the libraries handle on your behalf that are a pain in the keester to deal with on your own.

jQuery, in my opinion, has the easiest-to-use Ajax API (and you get a lot of other stuff that makes DOM-focused JavaScript so much easier to deal with).
 
Nick Potter
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bauke Scholtz wrote:You normally just use servlet / JSTL / EL. Just map a servlet on the desired URL, implement doGet() to load the list and set it in the request scope, use JSTL/EL in JSP to display the list.



How do I do that? I mean, if I map a servlet on "page.jsp" but i also have a jsp called "page.jsp" don't i get an exception? What am i missing?
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic