File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes Using a jQuery widget on a html <ul> made with java scriptlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Using a jQuery widget on a html <ul> made with java scriptlets" Watch "Using a jQuery widget on a html <ul> made with java scriptlets" New topic
Author

Using a jQuery widget on a html <ul> made with java scriptlets

Stamin Adrian
Ranch Hand

Joined: Dec 21, 2012
Posts: 34

I have the following jsp page that loads an unordered list dinamically from the database using scriptlets for iterating.


I want to use jQuery sortable widget on the list.The problem is it will only work for the first list item, since the others are loaded dinamically.
How would I make it work for all of them? This is the sortable widget http://jqueryui.com/sortable/

This would be the code snippet for the widget.Somehow I need to call it after the whole list has completed loading.
Can it be done? Any jQuery gurus around here? Any ideea will be highly apreciated.Thanks!


Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61010
    
  65

Stamin Adrian wrote:The problem is it will only work for the first list item, since the others are loaded dinamically.

That is not correct. The fact that the elements are created by scriptlets is completely irrelevant. All JSP scriptlets execute on the server in order to format a complete HTML page that is sent to the browser. And that is where JavaScript, including jQuery, is executed.

Please read this article to understand how JSP operates.

The actual problem is that your HTML is invalid. To see the HTML that jQuery is dealing with, you can do a View Source in the browser, or open up the debugger in the browser.

If you do, you will see that you have multiple elements with the same id value. That is not valid. Any id value in an HTML page must be unique to one, and only one, element.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61010
    
  65

P.S. Why are you still using scriptlets in 2012? They've been discredited and obsolete for over 10 years. See this JspFaq entry.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61010
    
  65

P.P.S. Since id values need to be unique, if you want to categorize multiple elements together, you'd use a class name, not an id.
Stamin Adrian
Ranch Hand

Joined: Dec 21, 2012
Posts: 34

I know it's obsolete, but I didn't know how to do it any other way.I'm making the project for a course i'm taking.I will learn jsf after this.So the problem was that the ul was inside the for.I was unaware of that.Stupid mistake.I just saw that now.Thanks.Do you know any sources from where I can learn modern jsp or jsf for that matter?
Stamin Adrian
Ranch Hand

Joined: Dec 21, 2012
Posts: 34

Instead of using scriptlets I should create those methods on the myUser bean and call them from there? How would you do this without scriptlets?
Stamin Adrian
Ranch Hand

Joined: Dec 21, 2012
Posts: 34

It is still not working.I know ids are unique.I have the id on the <ul> tag not on the <li> tag.There are not multiple ids with the same name.That is not the issue.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61010
    
  65

Stamin Adrian wrote:I will learn jsf after this

Who said anything about JSF? You should be using JSTL and EL in your JSP pages. JSF is a completely different framework (and not one of which I am a fan).

Stamin Adrian wrote:Do you know any sources from where I can learn modern jsp or jsf for that matter?

Ignore JSF until much much later (if at all). For JSP, the Head First Servlets and JSP book is popular.

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61010
    
  65

We need to see the rendered HTML for the page. Looking at the server-side code is not helpful in debugging cleint-side problems. Do a View Source or use the browser's debugger to inspect the HTML.
Stamin Adrian
Ranch Hand

Joined: Dec 21, 2012
Posts: 34

I can't really see the source of the page because I have imported pages with JSTL. Is there a way I could see the source somehow?The source shows me my empty divs.I used ajax a lot.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61010
    
  65

Bear Bibeault wrote:Do a View Source or use the browser's debugger to inspect the HTML.


To see the dynamic DOM, use the browser's debugger.
Stamin Adrian
Ranch Hand

Joined: Dec 21, 2012
Posts: 34

Thanks for all the helpfull messages!
 
jQuery in Action, 2nd edition
 
subject: Using a jQuery widget on a html <ul> made with java scriptlets