On Sun, Jul 12, 2009 at 12:46 PM, mnaveed<mnaveed...@gmail.com> wrote:
>
> Hi,
> I am new to JQuery, can anyone help how to generate dynamic links. I
> have a loop which generate some links and for each, an onclick event
> is attached, calling a javascript method with some arguments. some
> thing like this,
>
> for(x = 0; x<items.length; x++)
>  <a href="#" onclick="getData(items[x].id);" >Get data </a>
>
> how can i generate such a grid using Jquery. Actually i have to
> display an overlay on each click, and data will be loaded dynamically
> in the overlay, so how can i pass the ID to the method?
>

Do you want to create the links? Where do you want them appended? One
after the other? Or should they be appended to individual elements? If
the latter, and let's say you have a structure like so:

<div class="Items" id="item_0">foo </div>
<div class="Items" id="item_1">foo </div>
<div class="Items" id="item_2">foo </div>
<div class="Items" id="item_3">foo </div>
<div class="Items" id="item_4">foo </div>

... you needn't create the loop yourself. You could do something like:

$(function()
{
        $('.Items').each(function()
        {
                $('<a href="#" title="click me for data">get data</a>')
                        .click(function()
                        {
                                alert($(this).parent().attr('id'));
                        })
                        .appendTo($(this));
        });
});

Note that "this" inside the click handler refers to the link, while
the appendTo($(this)) refers to the div.

Reply via email to