Lucas_Santos
Lucas_Santos

Reputation: 4740

Recovering ID of a specific image

How can I get the ID of an image according to the following situation

JQUERY

$(".favoritar").click(function (data) {
    // Here I want recover the previous IMG ID, that in this case should be
    // DannaWhite, but the following code returns <img src="../../fotos_teste/05.PNG" id="DannaWhite" width="50" height="50" />
    alert($(this).parent().closest(".bxFotoUsuarioLateral").html());
});

HTML

<div class="modal-header" style="background-color: #0F4265 !important;">
    <div class="bxFotoUsuarioLateral">
        <img src="../../fotos_teste/05.PNG" id="DannaWhite" width="50" height="50" />
    </div>

    <div class="icon-star"></div>     

    <img src="../../Images/favorito.png" class="favoritar" id="favoritarPost1" style="float: right; cursor: pointer;" />
    <h4 class="modal-title" style="margin-left: 70px;">Cancelamento de Contrato <span id="idImovelAlteraStatus"></span></h4>
    <span class="txtSample05" style="text-transform: none; margin-left: 40px;">Há 2 horas</span>
</div>

Upvotes: 0

Views: 51

Answers (3)

user1977055
user1977055

Reputation: 1

Working example

$(".favoritar").click(function (data) {
  // Here I want recover the previous IMG ID, that in this case should be
  // DannaWhite, but the following code returns undefined
  alert( $(this).parents(".modal-header").find(".bxFotoUsuarioLateral img").attr("id"))
});

Upvotes: 0

Johan
Johan

Reputation: 35194

If it's only one image:

var id = $(this).closest('div.modal-header')
                .find('.bxFotoUsuarioLateral > img').prop('id');

If you want an array of ids, regardless of the amount of images:

var ids = $(this).closest('div.modal-header')
                .find('.bxFotoUsuarioLateral > img')
                .map(function(){
                    return this.id;
                }).get();

Upvotes: 1

Milind Anantwar
Milind Anantwar

Reputation: 82231

You can use:

$(this).closest(".modal-header").find(".bxFotoUsuarioLateral img").attr("id");

or

$(this).parents(".modal-header").find(".bxFotoUsuarioLateral img").attr("id");

Demo

Upvotes: 0

Related Questions