Remove attribute from a DIV only in mobile version

Asked

Viewed 767 times

0

I would like to remvover the href of an element only in the mobile version, which would be at 769px, the href that I would like to remove is from a menu, this menu is the same as the desktop version, only gets another css when entering at 769px, I tried to add a new class when entering 769px to be able to modify this href without having to go through the desktop menu, only it doesn’t work, when it comes back the desktop version is with href removed.

That class .menu-mobile only exists at 769px and disappears after passing 769px, but since the menu is the same in both sizes it only gets one more class, I believe this is the reason why it doesn’t work, because it comes back it only loses the class, but the change is already made so losing this class has no effect whatsoever. How could I fix this? Remove href only at 769px.

Code I tried to use

$j(document).ready(function() {
    $j('.menu-mobile a').removeAttr('href');
});

3 answers

1


This code will work in modern browsers. It adds (or maintains) the hrefs when resolution is greater than or equal to 769px and removes them when it is less than or equal to that, whenever the page is charged or resized. I created an array to store hrefs and retrieve them when necessary:

temp_href = [];
$(window).on("load resize", function(){
    els = $(".menu-mobile a");
    if(window.innerWidth <= 769){
        for(x=0;x<els.length;x++){
            if(temp_href.length != els.length){
                temp_href.push(els.eq(x).attr("href"));
            }
            els.eq(x).removeAttr('href');
        }
    }else if(temp_href.length != 0){
        for(x=0;x<els.length;x++){
            els.eq(x).attr('href',temp_href[x]);
        }
    }
});

temp_href = [];
$(window).on("load resize", function(){
	els = $(".menu-mobile a");
	if(window.innerWidth <= 769){
		for(x=0;x<els.length;x++){
			if(temp_href.length != els.length){
				temp_href.push(els.eq(x).attr("href"));
			}
			els.eq(x).removeAttr('href');
		}
	}else if(temp_href.length != 0){
		for(x=0;x<els.length;x++){
			els.eq(x).attr('href',temp_href[x]);
		}
	}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="menu-mobile">
	<a href="#1">Link 1</a>
	<br />
	<a href="#2">Link 2</a>
</div>

  • Face exactly this thank you so much!! I needed to save the value to be able to use it again while resizing the screen, thanks.

0

Use "window.innerWidth" it will pick up the total site size.

$(document).ready(function() {  
   var windowWidth = window.innerWidth;

   if(windowWidth <= 769) {
     $('.menu-mobile a').removeAttr('href');
   }
});

Try that code then:

function eDesktop() {
  if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
    return false;
  } else {
    return true;
  }
}

if(!eDesktop){
  $('.menu-mobile a').removeAttr('href')
}

jQuery(document).ready(function($) {
      if(!/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) )         {
         
          $('.teste2').removeClass('teste');
       }
 });
.teste {
width: 500px;
height: 500px;
background: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="teste teste2">
</div>

  • I have tried this way, it removes if you open in the mobile version and if you open in the desktop version is there, "works" but I would like in addition to the function to open on that screen if I made the transition from one screen to another worked as well. If you read the question (I don’t know if you got confused) you will see that the problem is when making the transition. If you pick up manually and decrease the screen size until you reach the mobile version and vice versa.

  • Try the code I changed

  • just take the exclamation off if, it is there to show that it works to deny for desktop

  • Here the code didn’t work, like the first one you sent works, it removes the href only on the 769px screen, but this I also managed to do, what I wanted was that when transiting(dragging until the pixel decrease) between the screen sizes he added and removed the href, but as the menu is done by backend(php) it builds it in a way that I can’t just put a if windowWidth <= 769 &#xA;$j('#a').removeAttr('href');&#xA;else &#xA;$j('#a').attr('href="valorinicial"');

  • In this case, your js code needs to run when your site has finished loading, because when your php runs js after and removes the link, you can play your script at the end of your site, after the menu generated by php.

0

Only complementing, to get control of browser resizing just use the method resize of jquery, example

$(document).ready(function() {  
   windowWidth = window.innerWidth;

   if(windowWidth <= 769) {
     $('.menu-mobile a').removeAttr('href');
   }

function onResize(){
   windowWidth = window.innerWidth;

   if(windowWidth <= 769) {
     $('.menu-mobile a').removeAttr('href');
   }
                   }

$(window).on('resize', onResize);

});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.