0

I have a search form that can display a list of persons matching the searching filters. Once the search results are displayed, I want a link (id=names_list) that, when clicked, displays a dialog box displaying the list of the persons names.

I don't understand why this code is not working :

 <a id="names_list" href="#">Names list</a>

    {% for person in persons %}
       <a class="name_for_mylist">{{ person | name }}</a>
    {% endfor %}

    <script>        
            $(document).ready(function()    
            {
                $("#names_list").click(function(event){
                    event.preventDefault();
                    var list = "";
                    $(".name_for_mylist").each(function(){
                        list += $(this).html() + "; ";
                    });
                    var box=list.dialog({ title: "Names list" });
                    box.show();
                });
            });
    </script>

Thanks a lot for your help!

3
  • 1
    The jQuery UI .dialog() function has to be applied to an HTML element in the DOM, not a string. Commented Aug 26, 2012 at 15:14
  • thanks for the reply, so how to pass this list variable into my dialog content? Commented Aug 26, 2012 at 15:22
  • See the answer from xdazz for an example. Commented Aug 26, 2012 at 15:23

3 Answers 3

1

list is a string, and it doesn't have the method .dialog.

You should do like below:

$(document).ready(function () {
    $("#names_list").click(function (event) {
        event.preventDefault();
        var list = "";
        $(".name_for_mylist").each(function () {
            list += $(this).html() + "; ";
        });
        $('<div>').html(list).appendTo('body').dialog({
            title: "Names list"
        });
    });
});
Sign up to request clarification or add additional context in comments.

Comments

0
var liste = "";
$(".name_for_mylist").each(function(){
  list += $(this).html() + "; ";
});

u declared liste variable and you are adding $(this).html() to list variable.

try changing list+= $(this).html()+ ';'; to liste+= $(this).html()+ ';';

1 Comment

The typo was fixed very shortly after the question was posted.
0

change this

var box=list.dialog({ title: "Names list" });

to this

var box=$(list).dialog({ title: "Names list" });

You can put damn near anything in the jquery wrapper.

About half way through your javascript,

var liste = "";

Extra "e" at the end of "list"

1 Comment

Yes sorry but it was just absent-mindedness on my part when writing the code on stack overflow :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.