• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Stamin Adrian
Ranch Hand
Posts: 34
Chrome Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
Chrome Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
Chrome Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
Chrome Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
Chrome Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
Chrome Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all the helpfull messages!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic