PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Mon Jun 24, 2019 9:33 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Wed Feb 03, 2016 3:37 pm 
Offline
Forum Commoner
User avatar

Joined: Thu Jul 03, 2014 12:35 pm
Posts: 65
Location: Indonesian
this my js code:
Syntax: [ Download ] [ Hide ]
        $.ajax({
                type: "POST",
            url: "foo.php",
            data: {
                param: "foo",
            },
            success: function(data) {
                var rest = data.data;
                if (rest.length > 0) {
                                        for (var i = 0; i < rest.length; i++) {
                                                var prestult = [
                                                        {
                                                                Product: foo,
                                                                Action: '<button type="button" id="gotobook'+i+'">foo</button>',
                                                        },
                                                ];
                                                DataTable.rows.add(prestult).draw();
                                            $('#gotobook' + i).click( function(){
                                              alert('you clicked ' + i);
                                            });
                                        };
                                };
            }
        })
            ).done( function(a1){
                var data = a1[0].data.length;
                if (data > 0) {
                        $('#loading-search').fadeOut('slow');
                } else {
                        $('#loading-search').fadeOut('slow');
                        $('.dataTables_empty').show();
                        $('#ifnodata').fadeIn('slow');
                }
                console.log(data);
            });
 


why if i click this button, i get wrong button id ? help me please...

Thanks Very Much...

_________________
Newbie The Passion for Learning


Top
 Profile  
 
PostPosted: Wed Feb 03, 2016 4:50 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13590
Location: New York, NY, US
The button has the id gotobook'1' while the jQuery click function attaches to #gotobook1.

_________________
(#10850)


Top
 Profile  
 
PostPosted: Thu Feb 04, 2016 2:57 am 
Offline
Forum Commoner
User avatar

Joined: Thu Jul 03, 2014 12:35 pm
Posts: 65
Location: Indonesian
Christopher wrote:
The button has the id gotobook'1' while the jQuery click function attaches to #gotobook1.

if i try inspect element, button id is correct
Syntax: [ Download ] [ Hide ]
id="gotobook1"

_________________
Newbie The Passion for Learning


Top
 Profile  
 
PostPosted: Thu Feb 04, 2016 12:36 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13590
Location: New York, NY, US
The event handlers may not be able to see the buttons you added. Check for the syntax for live mode for .on() or .click().

_________________
(#10850)


Top
 Profile  
 
PostPosted: Tue Apr 05, 2016 11:30 am 
Offline
Forum Contributor

Joined: Fri Jul 20, 2007 11:29 am
Posts: 367
I can't say exactly why but I'd bet if you rearranged your code a bit it might work.

Instead of an anonymous function on "success:", actuall define that function and assign its name to success.

instead of id=gotobook1 just add a data attribute to the button tag: data-id=1.

Instead of creating multiple anonymous click even handlers in the for-loop, create just 1 function
and assign that to a parent tag what wraps the area where all of these buttons will be.

https://learn.jquery.com/events/event-delegation/

Let the click event handler function on the parent tag determine if its child or grandchild that was clicked is a button or not.
If it's a button, read that data-id attribute from the clicked button and work with that value.

I assume all the buttons do exactly the same job so they only need a single click handler function instead of a new (but identical) anonymous function created _for_each_ button.

_________________
Warning: I have no idea what I'm talking about.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group