0

I want to change a style of the .check-media but this does not work. How to fix it?

<!DOCTYPE html>
<html>
<head>
<style>
    .check-media{
        width:500px;
        background-color:gray;
    }
</style>

<script src="http://code.jquery.com/jquery-1.12.0.min.js"></script>

<script>
    if($('.check-media').width() <= 768) {
        console.log('You are in width: 768px');
    }else{
        console.log('You are out of width: 768px');
    }

    if($(window).width() < 768) {
        console.log('screen.width is LESS then 768px');
        $('.check-media').css({"background-color":"blue"});
    } else {
        console.log('screen.width is MORE then 768px');
        $('.check-media').css({"background-color":"red"});
    }
</script>

</head>

<body>

<h1>My First Heading</h1>
<div class='check-media'>check-media contentn</div>
<p>My first paragraph.</p>

</body>
</html>
5
  • 1
    Wrap you code in document-ready handler. learn.jquery.com/using-jquery-core/document-ready Commented Jan 14, 2016 at 9:46
  • @Satpal it works but how to make it dynamicaly? Commented Jan 14, 2016 at 9:49
  • @rozero what did you mean by dynamic? Commented Jan 14, 2016 at 9:49
  • @RinoRaj depending of changing browser screen with Commented Jan 14, 2016 at 9:50
  • 1
    Assuming you want to amend the layout of the page based on the screen size, use CSS media queries as they were designed for this exact purpose. Commented Jan 14, 2016 at 9:53

4 Answers 4

2

Your code is executed before the subsequent <div class='check-media'></div> is even rendered (it is below in the DOM). You need to wait, until the DOM is ready (loaded) and then you can change it.

To do this, you can use the following snippet (requires jQuery):

$(document).ready(function() {
   // your code here
});
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, then you need to listen to the resize event. This can be solved by using $(window).resize(function() { ... } ); like @Maddy just answered. But take also a look at the CSS @media queries. This is much simpler done with plain CSS (e.g. w3schools.com/cssref/css3_pr_mediaquery.asp)
2

You need to add script in $( window ).resize(function()

Or

add script in bottom means after body.

$( window ).resize(function() {
    if($('.check-media').width() <= 768) {
        console.log('You are in width: 768px');
    }else{
        console.log('You are out of width: 768px');
    }

    if($(window).width() < 768) {
        console.log('screen.width is LESS then 768px');
        $('.check-media').css({"background-color":"blue"});
    } else {
        console.log('screen.width is MORE then 768px');
        $('.check-media').css({"background-color":"red"});
    }
    });
  .check-media{
        width:500px;
        background-color:gray;
    }
<script src="http://code.jquery.com/jquery-1.12.0.min.js"></script>

<h1>My First Heading</h1>
<div class='check-media'>check-media contentn</div>
<p>My first paragraph.</p>

Comments

0

You can achieve this using CSS alone via Media Queries:

@media (max-width: 768px) {
  .check-media {
    background-color: blue !important;
  }
}

.check-media {
    background-color: red;
}

Example fiddle - change window size to see it in action.

Relying on Javascript to amend the UI is considered bad practice.

Comments

0

Write your script inside.

$(document).ready(function(){
    write script here!
})

Or, check the code below.

<!DOCTYPE html>
<html>
<head>
<style>
    .check-media{
        width:500px;
        background-color:gray;
    }
</style>

<script src="http://code.jquery.com/jquery-1.12.0.min.js"></script>

<script>
  $(document).ready(function(){
    if($('.check-media').width() <= 768) {
        console.log('You are in width: 768px');
    }else{
        console.log('You are out of width: 768px');
    }

    if($(window).width() < 768) {
        console.log('screen.width is LESS then 768px');
        $('.check-media').css({"background-color":"blue"});
    } else {
        console.log('screen.width is MORE then 768px');
        $('.check-media').css({"background-color":"red"});
    }
    });
</script>

</head>

<body>

<h1>My First Heading</h1>
<div class='check-media'>check-media contentn</div>
<p>My first paragraph.</p>

</body>
</html>

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.