JVG
JVG

Reputation: 21150

Using $(this) with jQuery

I'm still a jquery/javascript newbie, so forgive me if this is an obvious one.

I'm making an artist bio page for a conference (it's up here in live view http://www.thinkinc.org.au/artists.html ). When you click an artist's image, a colorbox opens up with artist bio and videos. When clicking the thumbnails, I want to send a .remove() command to the initial video ("initvid") of the speaker the user is viewing.

The offending javascript code is here:

 $(document).ready(function () {
        $('.thumbnails img').click(function () {
            $('#initvid')+$(this).attr('id').remove(); /* I want this to get #initvid + the id of the thumbnail image that was clicked (to make #initvidtyson or #initvidhitchens) then .remove() it */

            $('#vidContainer').html('<iframe id="playingMovie" width="480" height="390" src="' + $(this).attr('toobId') +'" frameborder="0" allowfullscreen></iframe>');

            $('#vidContainer iframe').fadeToggle(400);
        });
    });

If you look at the code for my thumbnails, each has an id for the artist whose bio they belong to. (e.g. ; ). I want to create a jquery function that removes #initvidhitchens or #initvidtyson (depending on which bio thumbs are being clicked).

Probably missing something quite obvious, but why isn't my current code working?

Upvotes: 0

Views: 145

Answers (3)

Edgar Villegas Alvarado
Edgar Villegas Alvarado

Reputation: 18344

It should be like this:

$('#initvid' + $(this).attr('id')).remove();

or this:

$('#initvid'+this.id).remove();

Your code wasn't working because:

 $('#initvid') + //Here you're getting element with id 'initvid', which doesn't exist
 $(this).attr('id').remove();  //The result of getting id attribute wasn't a jQuery object, so it doesn't have 'remove' method

Hope this helps

Upvotes: 3

user166390
user166390

Reputation:

Close. The string passed to $ is what needs to have the extra identifier concatenated.

$('#initvid' + $(this).attr('id')).remove();

Think of it as:

$('#initvid' + extra) // the *result* of the string concat is used as the selector

However, depending upon exact problem, there may be cleaner methods to accomplish this task.

Happy coding.

Upvotes: 1

Phil
Phil

Reputation: 164731

$('#initvid' + $(this).attr('id')).remove();

Upvotes: 2

Related Questions