mamdouh alramadan
mamdouh alramadan

Reputation: 8528

jquery find img is returning undefined in special case

to describe my case, here's what I have:

javascript code:

$("#tf_zoom").live("click",function() {

    console.log($("#dynam").attr("src"));
}

here I get the value in the first time but after the slider changes the image I get undefined

I also tried:

console.log($(this).find("img").attr("src"));

but this one is not working at all.

html code:

<div id="tf_thumbs" class="tf_thumbs">
            <span id="tf_zoom" class="tf_zoom"></span>
            <img id="dynam" src="http://localhost/mala/assets/uploads/files/c9ab2-1.jpg" alt="Thumb1"/>
        </div>

the problem as I mentioned is that the slider changes the image and modifies the img tag to a new src attribute.

so what should I do.

any help would be greatly appreciated.

Upvotes: 1

Views: 545

Answers (1)

mamdouh alramadan
mamdouh alramadan

Reputation: 8528

As @François Wahl mentioned in his comment that I am using #tf_zoom as a parent for my image tag which is where I went wrong. so, I edited my code to use .tf_thumbs as the parent of my image tag and now it's working.

 $("#tf_zoom").live("click",function() {

    var n = $(".tf_thumbs").find("img").attr("src");
    //var imagespathes = {};
    //console.log($(".tf_thumbs").find("img").attr("src"));
    var modelid = n.substr(43); 


    $.post("models/get_gallery", { "modelid": modelid },
  function(data){
        //console.log(data);
        var imagespathes = $(data).map(function(key, url){
        return({ href: '<?php echo base_url();?>assets/uploads/files/' + url });
      });
      console.log(imagespathes);
  $.fancybox.open(imagespathes);
  }, "json");

            });

Upvotes: 2

Related Questions