0

I am trying to get Image ALT values on click event but it's not working as it should be and displays the same value (Image ALT) on click.

Here is the code:

HTML:

<a href="//cdn.shopify.com/s/files/1/0720/9527/products/MDM02-south-east-postcode-district-map-detail-large_ee8e2826-b33b-4c77-83b2-f162df33ec33_large.gif?v=1420644155" class="product-photo-thumb" title="Map 1">
    <img id="imgVariants" src="//cdn.shopify.com/s/files/1/0720/9527/products/MDM02-south-east-postcode-district-map-detail-large_ee8e2826-b33b-4c77-83b2-f162df33ec33_compact.gif?v=1420644155" alt="Map 1">
</a>

<a href="//cdn.shopify.com/s/files/1/0720/9527/products/png_large.png?v=1420644163" class="product-photo-thumb" title="Map 2">
    <img id="imgVariants" src="//cdn.shopify.com/s/files/1/0720/9527/products/png_compact.png?v=1420644163" alt="Map 2">
</a>

Javascript / jQuery

$(document).ready(function(){
    $('.product-photo-thumb').on("click", function (e) {
      e.preventDefault();
      alert(document.getElementById("imgVariants").alt);
    });
});

>> jsFiddle link

Am I missing anything?


P.S.: I know IDs must be unique but I wanted a solution that can work even without unique IDs.

6
  • 4
    IDs must be unique on document context... Commented Jan 8, 2015 at 9:49
  • 1
    ...and to explain why: Browsers keep a fast lookup dictionary for IDs, which contain a single DOM element against each ID value. This means that document.getElementById (and therefore jQuery also) can only see the first occurring element with a specific ID. Commented Jan 8, 2015 at 9:54
  • @A.Wolff I know IDs must be unique but I wanted a solution that can work even without unique IDs. Commented Jan 8, 2015 at 9:59
  • @DipakG. that can work even without unique IDs Why that? This doesn't make sense at all... Commented Jan 8, 2015 at 10:15
  • @A.Wolff Have you seen the answers posted below? It works without having unique IDs. Commented Jan 8, 2015 at 10:17

3 Answers 3

2

There are two elements with same Id(shouldnt be), remove them and you should use something like e.target

here is an working example.. http://jsfiddle.net/83cyuozz/1/

   $(document).ready(function(){
        $('.product-photo-thumb').on("click", function (e) {
          e.preventDefault();
          alert(e.target.alt);
        });
      });
Sign up to request clarification or add additional context in comments.

Comments

2

try this code

$(document).ready(function(){
    $('.product-photo-thumb').on("click", function (e) {
      e.preventDefault();
      alert($(this).find('img').attr('alt'));
    });
});

DEMO

NOTE ID must be unique.

Comments

2

Here is the working example where there can be multiple elements with same ID, and yet it will work fine: JSFIDDLE

  $(document).ready(function(){
    $("body").on("click",".product-photo-thumb", function (e) {
      e.preventDefault();

      alert($(this).children("img").attr("alt"));
    });
  });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.