Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-28-2013, 02:03 AM   PM User | #1
eydg
New Coder

 
Join Date: Sep 2012
Posts: 70
Thanks: 1
Thanked 1 Time in 1 Post
eydg is an unknown quantity at this point
SOLVED: js vs CSS condition: hide a class of elements when screen small

the following JS does work, somehow, but i have a sense that something is inherently wrong with the condition. could you pls help me sort it out?

Here's the simple html. I want the age distinction (adult vs child) to be out of the screen when it can not contain too many elements.


Code:
             <div  align="center">
                       <select data-role="slider">
                        <option value="off" >man</option>
                        <option value="on"> woman</option>
                      </select>
                    </div></td>
    <td>                    <div class="bigga"  align="center">
                      <select data-role="slider" >
                          <option value="off">adult</option>
                        <option value="on"> child</option>
                      </select>

and here is the unelegant js:


Code:
$(document).ready(function() {
if ((screen.width>=240) )
{
 $('.bigga').hide();
}
if ((screen.width>=320) )
{
 $('.bigga').show();
}
});
First of all, all the trick should be doable with just one condition. By logic, this condition should look as follows:


Code:
$(document).ready(function() {
if ((screen.width<=240) )
{
 $('.bigga').hide();
}
});
This would mean that when the screen width is 240 or smaller, the "bigga" clas of elements does not show up.
Why does it not work?...

Last edited by eydg; 01-29-2013 at 04:02 AM.. Reason: changed title to jQuery
eydg is offline   Reply With Quote
Old 01-28-2013, 04:30 PM   PM User | #2
sunfighter
Senior Coder

 
Join Date: Jan 2011
Location: Missouri
Posts: 2,365
Thanks: 18
Thanked 348 Times in 347 Posts
sunfighter is on a distinguished road
I hope you are doing this on a mobile device eydg, because the width of your screen, ie; your monitor, does not change. My screen is 1440 px wide so this does work on my screen:
Code:
if(screen.width <= 1440) $('.bigga').hide();
And your code should work if your checking it on a suitable device. Maybe you should use the size of 320 instead of 240.

A good place to look maybe https://developer.mozilla.org/en-US/.../Media_queries and use css instead of jquery.

If you are not working with a cell or a tablet you need to use the width of the browser.

Last edited by sunfighter; 01-28-2013 at 04:34 PM..
sunfighter is offline   Reply With Quote
Old 01-28-2013, 11:40 PM   PM User | #3
eydg
New Coder

 
Join Date: Sep 2012
Posts: 70
Thanks: 1
Thanked 1 Time in 1 Post
eydg is an unknown quantity at this point
yes, i need it to work with phones too.

media queries - if i understand the linked tutorials - involve several different css'es applied depending on various conditions, like f e screen resolution.

this would greatly complicate future reediting. If possible at all, i would like to stay with js.

it appears to work. thank you.
eydg is offline   Reply With Quote
Old 01-29-2013, 01:14 AM   PM User | #4
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,452
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
Quote:
Originally Posted by eydg View Post
yes, i need it to work with phones too.

media queries - if i understand the linked tutorials - involve several different css'es applied depending on various conditions, like f e screen resolution.

this would greatly complicate future reediting. If possible at all, i would like to stay with js.

it appears to work. thank you.
my $0.02: i think putting CSS into JS would greatly complicate future reediting. CSS belongs in CSS files, where it controls presentation. JS is for behavior, and behavior, content, and presentation should be kept separate.

all you would need to do is define your small screen class inside a small screen media query.



i like to use min/max for everything for greater cut and pastability:
Code:
/* between 1 and 250px wide :*/
@media screen and (min-width:1) and (max-width:250) {
	.bigga { display: none; }
}
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.8% IE9:11.4% IE10:6.5%
rnd me is offline   Reply With Quote
Old 01-29-2013, 03:59 AM   PM User | #5
eydg
New Coder

 
Join Date: Sep 2012
Posts: 70
Thanks: 1
Thanked 1 Time in 1 Post
eydg is an unknown quantity at this point
it does work. thank you. just needed to add px.

appears to be a recurrent motive, so to sum up, in case someone needs it:

html content
css presentation
js behaviour
eydg is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:20 AM.


Advertisement
Log in to turn off these ads.