Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

jQuery droppable only accepting first list item.

Posted on 2009-05-12
5
Medium Priority
?
1,176 Views
Last Modified: 2013-11-11
I have a list that I am appending to a drop box. You can drag the list items into the drop box and it should accept them. Well, it is only accepting the first item in the list.

This used to be working but I updated my jquery.ui files and now it doesn't work.

I have tested many different ways of writing this code.

You can take a look at it here:
http://dalelarsen.com/fab/drop.php

You can view the source to get the code.

Thanks in advance!!
0
Comment
Question by:Shaye Larsen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 18

Expert Comment

by:Pawel Witkowski
ID: 24372495
You need to specify all class in accept attibure and inside this function check on what element was selected instead of creating so many functions. jQuery implementaation  creates only one behaviour with .dropable  and any else call to it just beeing discarded :)
0
 
LVL 1

Author Comment

by:Shaye Larsen
ID: 24386848
@wilq32

Thank you. I fiddled around with it but I can't figure out how to create a double condition that will detect which element is being dropped in order to append the correct html to the drop box.

How do I get the class name of the element dropped?

I need it to be something like this:

if (theDroppedClass == ".about") && $(this).find('a.x_delete').length < 5)
then append this stuff...

else if ...

and the rest like so.

Here is the code right now unedited:
$(document).ready( function(){
$(".blank, .about, .articles, .calendar, .contact, .faq, .hours, .jobs, .news, .press, .support").draggable({helper: 'clone'});
    $(".pages_drop").droppable({
        accept: ".blank",
        activeClass: "droppable-active",
        drop: function(ev, ui) {
        if($(this).find('a.x_delete').length < 5)
        var el=$("<input type=\"text\" value='Name Your Page'></input>&nbsp;<a class=\"x_delete\" href='#'>[x]</a>").filter('a').click(function(){el.remove()}).end().appendTo(this);
        }
    });
    $(".pages_drop").droppable({
        accept: ".about",
        activeClass: "droppable-active",
        drop: function(ev, ui) {
        if($(this).find('a.x_delete').length < 5)
        var el=$("<input type=\"text\" value='About'></input>&nbsp;<a class=\"x_delete\" href='#'>[x]</a>").filter('a').click(function(){el.remove()}).end().appendTo(this);
        }
    });
    $(".pages_drop").droppable({
        accept: ".articles",
        activeClass: "droppable-active",
        drop: function(ev, ui) {
        if($(this).find('a.x_delete').length < 5)
        var el=$("<input type=\"text\" value='Articles'></input>&nbsp;<a class=\"x_delete\" href='#'>[x]</a>").filter('a').click(function(){el.remove()}).end().appendTo(this);
        }
    });
    $(".pages_drop").droppable({
        accept: ".calendar",
        activeClass: "droppable-active",
        drop: function(ev, ui) {
        if($(this).find('a.x_delete').length < 5)
        var el=$("<input type=\"text\" value='Calendar'></input>&nbsp;<a class=\"x_delete\" href='#'>[x]</a>").filter('a').click(function(){el.remove()}).end().appendTo(this);
        }
    });
    $(".pages_drop").droppable({
        accept: ".contact",
        activeClass: "droppable-active",
        drop: function(ev, ui) {
        if($(this).find('a.x_delete').length < 5)
        var el=$("<input type=\"text\" value='Contact'></input>&nbsp;<a class=\"x_delete\" href='#'>[x]</a>").filter('a').click(function(){el.remove()}).end().appendTo(this);
        }
    });
    $(".pages_drop").droppable({
        accept: ".faq",
        activeClass: "droppable-active",
        drop: function(ev, ui) {
        if($(this).find('a.x_delete').length < 5)
        var el=$("<input type=\"text\" value='FAQ'></input>&nbsp;<a class=\"x_delete\" href='#'>[x]</a>").filter('a').click(function(){el.remove()}).end().appendTo(this);
        }
    });
    $(".pages_drop").droppable({
        accept: ".hours",
        activeClass: "droppable-active",
        drop: function(ev, ui) {
        if($(this).find('a.x_delete').length < 5)
        var el=$("<input type=\"text\" value='Hours'></input>&nbsp;<a class=\"x_delete\" href='#'>[x]</a>").filter('a').click(function(){el.remove()}).end().appendTo(this);
        }
    });
    $(".pages_drop").droppable({
        accept: ".jobs",
        activeClass: "droppable-active",
        drop: function(ev, ui) {
        if($(this).find('a.x_delete').length < 5)
        var el=$("<input type=\"text\" value='Jobs'></input>&nbsp;<a class=\"x_delete\" href='#'>[x]</a>").filter('a').click(function(){el.remove()}).end().appendTo(this);
        }
    });
    $(".pages_drop").droppable({
        accept: ".news",
        activeClass: "droppable-active",
        drop: function(ev, ui) {
        if($(this).find('a.x_delete').length < 5)
        var el=$("<input type=\"text\" value='News'></input>&nbsp;<a class=\"x_delete\" href='#'>[x]</a>").filter('a').click(function(){el.remove()}).end().appendTo(this);
        }
    });
    $(".pages_drop").droppable({
        accept: ".press",
        activeClass: "droppable-active",
        drop: function(ev, ui) {
        if($(this).find('a.x_delete').length < 5)
        var el=$("<input type=\"text\" value='Press'></input>&nbsp;<a class=\"x_delete\" href='#'>[x]</a>").filter('a').click(function(){el.remove()}).end().appendTo(this);
        }
    });
    $(".pages_drop").droppable({
        accept: ".support",
        activeClass: "droppable-active",
        drop: function(ev, ui) {
        if($(this).find('a.x_delete').length < 5)
        var el=$("<input type=\"text\" value='Support'></input>&nbsp;<a class=\"x_delete\" href='#'>[x]</a>").filter('a').click(function(){el.remove()}).end().appendTo(this);
        }
    });
});

Open in new window

0
 
LVL 18

Accepted Solution

by:
Pawel Witkowski earned 2000 total points
ID: 24393715
try this
$(".blank, .about, .articles, .calendar, .contact, .faq, .hours, .jobs, .news, .press, .support").draggable({helper: 'clone'});
    $(".pages_drop").droppable({
        accept: ".blank, .about, .articles, .calendar, .contact, .faq, .hours, .jobs, .news, .press, .support",
        activeClass: "droppable-active",
        drop: function(ev, ui) {
        var elementClass = ui.draggable[0].className.split(" ")[0];
        if($(this).find('a.x_delete').length < 5)
           {
                alert(elementClass);
           }
        }
    });

Open in new window

0
 
LVL 1

Author Comment

by:Shaye Larsen
ID: 24398598
Perfect that was just what I needed.

Here is the final code:
$(document).ready( function(){
$(".blank, .about, .articles, .calendar, .contact, .faq, .hours, .jobs, .news, .press, .support").draggable({helper: 'clone'});
    $(".pages_drop").droppable({
        accept: ".blank, .about, .articles, .calendar, .contact, .faq, .hours, .jobs, .news, .press, .support",
        activeClass: "droppable-active",
        drop: function(ev, ui) {
        var elementClass = ui.draggable[0].className.split(" ")[0];
		var dropLimit = 5;//this nuber controls the amount of items that can be dropped into the box
        if((elementClass == "blank")&&($(this).find('a.x_delete').length < dropLimit)) {
           var el=$("<input type=\"text\" value='Name Your Page'></input>&nbsp;<a class=\"x_delete\" href='#'>[x]</a>").filter('a').click(function(){el.remove()}).end().appendTo(this);
           }
        else if((elementClass == "about")&&($(this).find('a.x_delete').length < dropLimit)) {
           var el=$("<input type=\"text\" value='About'></input>&nbsp;<a class=\"x_delete\" href='#'>[x]</a>").filter('a').click(function(){el.remove()}).end().appendTo(this);
           }
        else if((elementClass == "articles")&&($(this).find('a.x_delete').length < dropLimit)) {
           var el=$("<input type=\"text\" value='Articles'></input>&nbsp;<a class=\"x_delete\" href='#'>[x]</a>").filter('a').click(function(){el.remove()}).end().appendTo(this);
           }
        else if((elementClass == "calendar")&&($(this).find('a.x_delete').length < dropLimit)) {
           var el=$("<input type=\"text\" value='Calendar'></input>&nbsp;<a class=\"x_delete\" href='#'>[x]</a>").filter('a').click(function(){el.remove()}).end().appendTo(this);
           }
        else if((elementClass == "contact")&&($(this).find('a.x_delete').length < dropLimit)) {
           var el=$("<input type=\"text\" value='Contact'></input>&nbsp;<a class=\"x_delete\" href='#'>[x]</a>").filter('a').click(function(){el.remove()}).end().appendTo(this);
           }
        else if((elementClass == "faq")&&($(this).find('a.x_delete').length < dropLimit)) {
           var el=$("<input type=\"text\" value='FAQ'></input>&nbsp;<a class=\"x_delete\" href='#'>[x]</a>").filter('a').click(function(){el.remove()}).end().appendTo(this);
           }
        else if((elementClass == "hours")&&($(this).find('a.x_delete').length < dropLimit)) {
           var el=$("<input type=\"text\" value='Hours'></input>&nbsp;<a class=\"x_delete\" href='#'>[x]</a>").filter('a').click(function(){el.remove()}).end().appendTo(this);
           }
        else if((elementClass == "jobs")&&($(this).find('a.x_delete').length < dropLimit)) {
           var el=$("<input type=\"text\" value='Jobs'></input>&nbsp;<a class=\"x_delete\" href='#'>[x]</a>").filter('a').click(function(){el.remove()}).end().appendTo(this);
           }
        else if((elementClass == "news")&&($(this).find('a.x_delete').length < dropLimit)) {
           var el=$("<input type=\"text\" value='News'></input>&nbsp;<a class=\"x_delete\" href='#'>[x]</a>").filter('a').click(function(){el.remove()}).end().appendTo(this);
           }
        else if((elementClass == "press")&&($(this).find('a.x_delete').length < dropLimit)) {
           var el=$("<input type=\"text\" value='Press'></input>&nbsp;<a class=\"x_delete\" href='#'>[x]</a>").filter('a').click(function(){el.remove()}).end().appendTo(this);
           }
        else if((elementClass == "support")&&($(this).find('a.x_delete').length < dropLimit)) {
           var el=$("<input type=\"text\" value='Support'></input>&nbsp;<a class=\"x_delete\" href='#'>[x]</a>").filter('a').click(function(){el.remove()}).end().appendTo(this);
           }
		else {
            alert('You can only add 5 pages to begin.');
           }
        }
    });
});

Open in new window

0
 
LVL 1

Author Closing Comment

by:Shaye Larsen
ID: 31580782
Thanks wilq32! Perfect solution! Genius!
0

Featured Post

RHCE - Red Hat OpenStack Prep Course

This course will provide in-depth training so that students who currently hold the EX200 & EX210 certifications can sit for the EX310 exam. Students will learn how to deploy & manage a full Red Hat environment with Ceph block storage, & integrate Ceph into other OpenStack service

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question