Domanda

For my example I have 3 a href tags. So if I click on the first a href tag so I like to pop the first event_id from the array variable in the console. How can I to solve this?

Here my HTML code:

<a href="#" event-id="" class="event_delete">Link</a>
<a href="#" event-id="" class="event_delete">Link</a>
<a href="#" event-id="" class="event_delete">Link</a>

Here my jQuery code:

var event_id = new Array(); 
    //event_id for the first a href tag
    event_id.push("1X4JxCOwhDpD4Oj5ch");
    //event_id for the second a href tag
    event_id.push("LKb77tAmVzeJJjE83LH");
    //event_id for the third a href tag
    event_id.push("h0NTcpfUlinWbpwBbpB1");


jQuery('.event_delete').click(function(){
    var index = jQuery('.event_delete').index(this);
    //..
    //console.log(event_id.pop(???))

})

JSFIDDLE DEMO

È stato utile?

Soluzione

It seems to me like you should associate the array IDs with the link's IDs

jQuery("a.event_delete").each( function(index, value){
    jQuery(this).attr('id', event_id[index]);
    console.log(this);
});

jQuery('.event_delete').click(function(){
    var index = jQuery('.event_delete').index(this);
    console.log(jQuery(this).attr('id'));
});

But you could just have the IDs in the HTML originally, unless you are getting them from another source later.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top