jQuery: The Write Less, Do More JavaScript Library

jQuery - Click function + Ajax

View: New views
3 Messages — Rating Filter:   Alert me  

jQuery - Click function + Ajax

by Glazz :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Hi there,

I have a hyperlink ( <a> ) after i click on that hyperlink, the script
sends some data to a .php page;

That works fine, but for just one hyperlink , i want to be able to
have 2 or more hyperlinks in the same page using the same script.

Here goes the script:
        $("#button").click(function(){
                var element = this;
                var productId = $(element).attr('product');
                //$(element).attr("href", "javascript: void(0);");
                $.ajax({
                   type: "GET",
                   url: "index.php",
                   data: "product=" + productId
                 });
        });

HTML:
<a id="button" rel="nofollow" product="10" href="#test">Product 10</a>
<a id="button" rel="nofollow" product="20" href="#test">Product 20</a>


Re: jQuery - Click function + Ajax

by lloyd mcclendon :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


You can't have 2 elements with the same id on the same page.  Instead
do <a class="button" and change your selector to use $("a.button").  I
would also recommend using <a href=""


I'm kind of doing the same thing (i have a thread about it), is it
safe to put event parameters in arbitrary attribute names?  i.e.: <td
class="trigger" record="${record.ID}" paramX="${paramX}" paramY="$
{paramY}">

isn't that invalid html and rejected by some browsers?  or is that
normal and just fine to do?


On Jul 2, 12:44 pm, Glazz <brunofgas...@...> wrote:

> Hi there,
>
> I have a hyperlink ( <a> ) after i click on that hyperlink, the script
> sends some data to a .php page;
>
> That works fine, but for just one hyperlink , i want to be able to
> have 2 or more hyperlinks in the same page using the same script.
>
> Here goes the script:
>         $("#button").click(function(){
>                 var element = this;
>                 var productId = $(element).attr('product');
>                 //$(element).attr("href", "javascript: void(0);");
>                 $.ajax({
>                    type: "GET",
>                    url: "index.php",
>                    data: "product=" + productId
>                  });
>         });
>
> HTML:
> <a id="button" rel="nofollow" product="10" href="#test">Product 10</a>
> <a id="button" rel="nofollow" product="20" href="#test">Product 20</a>

Re: jQuery - Click function + Ajax

by Glazz :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Off course, i forgot that hehe it is working great right now ;)

About that, i really don't know, sometimes i need to use it this way,
but you can try to validate using W3C tools

On 2 Jul, 17:55, John Newman <john.newma...@...> wrote:

> You can't have 2 elements with the same id on the same page.  Instead
> do <a class="button" and change your selector to use $("a.button").  I
> would also recommend using <a href=""
>
> I'm kind of doing the same thing (i have a thread about it), is it
> safe to put event parameters in arbitrary attribute names?  i.e.: <td
> class="trigger" record="${record.ID}" paramX="${paramX}" paramY="$
> {paramY}">
>
> isn't that invalid html and rejected by some browsers?  or is that
> normal and just fine to do?
>
> On Jul 2, 12:44 pm, Glazz <brunofgas...@...> wrote:
>
> > Hi there,
>
> > I have a hyperlink ( <a> ) after i click on that hyperlink, the script
> > sends some data to a .php page;
>
> > That works fine, but for just one hyperlink , i want to be able to
> > have 2 or more hyperlinks in the same page using the same script.
>
> > Here goes the script:
> >         $("#button").click(function(){
> >                 var element = this;
> >                 var productId = $(element).attr('product');
> >                 //$(element).attr("href", "javascript: void(0);");
> >                 $.ajax({
> >                    type: "GET",
> >                    url: "index.php",
> >                    data: "product=" + productId
> >                  });
> >         });
>
> > HTML:
> > <a id="button" rel="nofollow" product="10" href="#test">Product 10</a>
> > <a id="button" rel="nofollow" product="20" href="#test">Product 20</a>