0

I have 2 images in my html. 1 I show on the mobile version(if screen is smaller than 992px) and the other on desktop version.

I added an id to these images to use as an anchor. Everything works great on mobile devices but on the desktop version it doesn't wanna go to the anchor. Even though the id is there in the html.

This is what my images in html looks like:

<img id="wie" class="cool-image-2" src="source1" alt="">
<img id="wie" class="cool-image-2" src="source2" alt="">

These are the links: Dekstop menu:

<ul class="nav navbar-nav">
   <li class="navbar-right"><a data-id="wie" href="#wie">Wie</a></li>
</ul>

Mobile menu:

<ul class="nav mobile-nav-bar">
   <li class="navbar-right"><a data-id="cont" href="#contact">Contact</a></li>
</ul>

This is my jQuery:

jQuery(".nav li a").click(function(e) {
        e.preventDefault();
        $id = jQuery(this).attr("data-id");
        jQuery("a.clicked").removeClass("clicked");
        jQuery(this).addClass("clicked");
        console.log($id);

            jQuery('html, body').animate({
               scrollTop: jQuery("#"+$id).offset().top 
            }, 1000);

    });

Anyone has any idea why it doesn't work on desktop?

Many Thanks in advance!!

Frank Lucas
  • 582
  • 2
  • 12
  • 28

2 Answers2

0

You could not have two items with same ID. Use only one img and change the source depending on device. Use this to know if you're on mobile.

Community
  • 1
  • 1
bokan
  • 3,601
  • 2
  • 23
  • 38
0

You have two HTML elements with the same ID attribute "wie":

<img id="wie" class="cool-image-2" src="source1" alt="">
<img id="wie" class="cool-image-2" src="source2" alt="">

The ID attribute value must be unique within your HTML document.

https://www.w3.org/TR/html5/dom.html#the-id-attribute

The id attribute specifies its element's unique identifier (ID). The value must be unique amongst all the IDs in the element's home subtree and must contain at least one character. The value must not contain any space characters.

iamcryptoki
  • 446
  • 2
  • 4
  • 16