PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Sun Jun 16, 2019 3:05 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 8 posts ] 
Author Message
PostPosted: Tue Dec 15, 2015 2:58 pm 
Offline
Forum Contributor

Joined: Thu Apr 16, 2015 7:55 am
Posts: 110
Hi There,
I'm having a jqgrid. With each row has a link for delete.

On click of the delete link, want the custom attribute I've set.
custom attribute is the primary key for row item.

On click of the URL, that particular item will be deleted from the database, and grid.

Example:
Syntax: [ Download ] [ Hide ]
<a href="#" del_val="9" onclick=delete_pipeline()>Delete</a>
 


Syntax: [ Download ] [ Hide ]

$val=$(this).data('del_val'); //--data
alert($val); //undefined

$val=$(this).attr('del_val'); //--attr
alert($val); //undefined

 

I changed del_val in double quotes:
Syntax: [ Download ] [ Hide ]
<a href="#" "del_val"="5" onclick=delete_pipeline()>Delete</a>
 


Then again I've undefined.

How do I fix it?
Thanks.


Top
 Profile  
 
PostPosted: Tue Dec 15, 2015 8:41 pm 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6617
Location: WA, USA
attr() should have worked.

However you aren't doing this the best way.
1. Use custom data attributes as defined in HTML 5. That mean the "data-" prefix.
Code:
<a href="#" data-del-val="9">

2. Don't use onclick properties. Add the click handler using Javascript (ie, jQuery). You can do it really easily with
Syntax: [ Download ] [ Hide ]
$("a[data-del-val]").click(function() { // click handler for any <a> with a data-del-val attribute

(assuming the <a>s are not being dynamically generated or altered)
3. Get the del-val with .data().
Syntax: [ Download ] [ Hide ]
$val = $(this).data("del-val"); // will look for data-del-val


Top
 Profile  
 
PostPosted: Wed Dec 16, 2015 7:46 am 
Offline
Forum Contributor

Joined: Thu Apr 16, 2015 7:55 am
Posts: 110
requinix wrote:
attr() should have worked.

However you aren't doing this the best way.
1. Use custom data attributes as defined in HTML 5. That mean the "data-" prefix.
Code:
<a href="#" data-del-val="9">

2. Don't use onclick properties. Add the click handler using Javascript (ie, jQuery). You can do it really easily with
Syntax: [ Download ] [ Hide ]
$("a[data-del-val]").click(function() { // click handler for any <a> with a data-del-val attribute

(assuming the <a>s are not being dynamically generated or altered)
3. Get the del-val with .data().
Syntax: [ Download ] [ Hide ]
$val = $(this).data("del-val"); // will look for data-del-val



Hi requinix,
Thanks for your reply.

<a> are being added dynamically.
Is there another approach for dynamic ones?


Top
 Profile  
 
PostPosted: Wed Dec 16, 2015 8:33 am 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6425
Location: Montreal, Canada
Why would you need another approach?

_________________
Supported PHP versions No longer supported versions


Top
 Profile  
 
PostPosted: Wed Dec 16, 2015 8:35 am 
Offline
Forum Contributor

Joined: Thu Apr 16, 2015 7:55 am
Posts: 110
I think I got it working:

I added class to the dynamically added anchor tags:

Syntax: [ Download ] [ Hide ]
<a href="#" class="delete_pipe" data-del-val="12" >Delete</a>
 


Syntax: [ Download ] [ Hide ]
$('body').on('click','a.delete_pipe',function(){
//do stuff here

var id=$(this).data("del-val"); //works great....

 


I found about this from:

http://stackoverflow.com/questions/1359 ... l-elements

I hope this would be fine?


Top
 Profile  
 
PostPosted: Wed Dec 16, 2015 8:40 am 
Offline
Forum Contributor

Joined: Thu Apr 16, 2015 7:55 am
Posts: 110
Celauran wrote:
Why would you need another approach?

Hi Cel,
Thanks for your reply.

Syntax: [ Download ] [ Hide ]
        $("a[data-del-val]").click(function(){

                alert("yahoo");
                });
 


This (above) method didn't work if HTML (anchor tags I'm adding) are created post page loading.

Thus, I looked for different method. :)


Top
 Profile  
 
PostPosted: Wed Dec 16, 2015 2:29 pm 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6617
Location: WA, USA
publicGenome wrote:
I hope this would be fine?

Yeah, that's the dynamic version: using .on with a parent element and a selector.


Top
 Profile  
 
PostPosted: Wed Dec 16, 2015 2:38 pm 
Offline
Forum Contributor

Joined: Thu Apr 16, 2015 7:55 am
Posts: 110
requinix wrote:
publicGenome wrote:
I hope this would be fine?

Yeah, that's the dynamic version: using .on with a parent element and a selector.

Thank you. :)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 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:  
Powered by phpBB® Forum Software © phpBB Group