Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

davy_yg's avatar
Level 27

How to put on mobile condition

Hello,

I wonder how to put this code on mobile condition / view:

catalog.blade.php

	<script>

	$( ".list" ).hide();
	$( ".grid" ).show();

	...
	</script>

As you know the above code will appears on pc. On mobile I would like to show list and hide grid. How to write codes?

ref: https://developer.mozilla.org/en-US/search?q=mobile+view

0 likes
9 replies
martinbean's avatar

@davy_yg This is inefficient. It means on mobile, where connections may be slower or the user has a limited data plan, you’re making them download a web page that contains information they’re never going to see.

Use a responsive design instead that presents the information in the most optimal way for the viewport size, instead of sending duplicate content which will also get you penalised by search engines.

davy_yg's avatar
Level 27

@martinbean

The thing is the code is different on mobile and on pc. On PC I would like to content filter side by side with the product content. Whereas on mobile, the content filter and content are in a row. Therefore, I would like the list view on mobile automatically show up and I could delete the grid view.

martinbean's avatar

@davy_yg You do not need to duplicate HTML just to go from a two-column layout on larger screens to a stacked layout on smaller screens. You really need to look into responsive web design.

You should not be sending unnecessary HTML to a mobile device for multiple reasons:

  1. People on mobile may have plans with limited data. You’re eating their data allowance by sending them massive chunks of HMTL that they’ll never see if you just “hide” it on mobile devices.
  2. Putting the same content on one page multiple times is going to harm your SEO.
  3. Mobile devices aren’t as powerful as laptop or desktop computers, so sending them massive web pages with HTML and assets they don’t need is just wasting the processing power of the user’s device, and is going to drain their battery quicker.
Akash_kushwaha's avatar

You can do Something like that.

	$(document).ready(function() {
		if($(window).width() <= 768px) {
    		$( ".list" ).show();
			$( ".grid" ).hide();
	 	}
});
	
davy_yg's avatar
Level 27

@Akash_kit

	if($(window).width() > 768px) {
	
		$( ".list" ).show();
		$( ".grid" ).hide();
 	
 	}else{

 		$( ".list" ).hide();
		$( ".grid" ).show();
 	}

Now, both list and grid show up on PC.

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

@davy_yg please learn to read console errors. I'm sure it gives an error for 768px, which should be just 768

But why not just use css to hide it as Martin suggested.. You are rendering it anyways

Akash_kushwaha's avatar

@davy_yg Try this

	if ($(window).width() <= 767) {
	$( ".list" ).show();
	$( ".grid" ).hide();
}else{
	$( ".list" ).hide();
	$( ".grid" ).show();
}
Akash_kushwaha's avatar

@davy_yg Or better try using the condition with Resize event.

			$(window).resize(function(){
				if ($(window).width() <= 767) {
					$( ".list" ).show();
					$( ".grid" ).hide();
				}else{
					$( ".list" ).hide();
					$( ".grid" ).show();
				}
		});

Please or to participate in this conversation.