Nishant Ghodke
Nishant Ghodke

Reputation: 923

Pass ID of an element to jquery function

I want to fetch the id of an element and pass it in jquery function -

              $('#fetchedID').fadeOut;

Till now I have tried -

1.  $("#$('.delete_status').attr('id')").fadeOut(400);

2.  var e = $('.delete_status').attr('id');
                 $(e).fadeOut(400);

I am sure I am stuck because of the wrong syntax of passing javascript variable in jQuery function. Please help.

Upvotes: 0

Views: 3873

Answers (4)

Guilherme Sehn
Guilherme Sehn

Reputation: 6787

Do you really need to pick the ID to then reselect the element and do the fade? If you want to pick only the first occurence of your class, you can use :eq(0) instead.

$('.delete_status:eq(0)').fadeOut(400);

Upvotes: 0

Jesse T-Cofie
Jesse T-Cofie

Reputation: 227

$("#" + $('.delete_status').attr('id')).fadeOut(400);

Upvotes: 0

GautamD31
GautamD31

Reputation: 28753

Try with concating the Id that you have got with the Id selector(#) like

var e = $('.delete_status').attr('id');
$("#" + e).fadeOut(400);

Upvotes: 3

rink.attendant.6
rink.attendant.6

Reputation: 46257

You have to concatenate the selector, like this:

$("#" + $('.delete_status').prop('id')).fadeOut(400);

If you're going to be using the ID more than once, it is a good idea to cache it:

var delete_status_id = $('.delete_status').prop('id');
$("#" + delete_status_id ).fadeOut(400);
// do something else with delete_status_id...

Upvotes: 1

Related Questions