1

I have a problem with my code. I am trying to add a background color to div if data attribute is true. This is my HTML code:

<html data-highcontrast-applied="true">
<head>
</head>
<body>
  <div class="row-centered">
    <p>
    Helllllllooooooooooo
    </p>
  </div>
</body>
</html>

This is jQuery code

$(document ).ready(function() {

        if(typeof $("html").attr('highcontrast-applied')  == 'true')
        {
            $(".row-centered").css({"background-color": "red"});
        }

    });

Can somebody help me with this? Also this is a jsfiddle link: https://jsfiddle.net/dmv9o3ep/2/

mijok
  • 201
  • 2
  • 13
  • 1
    either use `$("html").attr('data-highcontrast-applied')` or `$("html").data('highcontrast-applied')` So you final `if` statement would be `if($("html").data('highcontrast-applied'))` – Carsten Løvbo Andersen Nov 20 '19 at 08:53

7 Answers7

2

try to remove typeof and get the value of this attribute like string then compare it with 'true'

$(document ).ready(function() {

    if($("html").attr('data-highcontrast-applied').toString() == 'true')
    {
        $(".row-centered").css({"background-color": "red"});
    }

});
Marwen Jaffel
  • 703
  • 1
  • 6
  • 14
1

The problems with your code are:

  1. You are not properly getting the value of the html attribute;

  2. typeof on $("html").attr('data-highcontrast-applied') will return boolean not the value of the html attribute;

I would set the data-* attribute to either 1 or 0 and then use parseInt and check if it's equal to 1 and apply the background (or any other css properties you want):

$(document).ready(function() {
  if (parseInt($("html").attr('data-highcontrast-applied')) === 1) {
    $(".row-centered").css({
      "background-color": "red"
    });
  }
});

https://jsfiddle.net/eht9faoz/

You can read more about jQuery .data() and .attr(): https://stackoverflow.com/a/7262427/867418

Ivanka Todorova
  • 9,964
  • 16
  • 66
  • 103
0

You misspelled the attribute name, try this:

$(document).ready(function() {
    if(typeof $("html").attr("data-highcontrast-applied"))
    {
        $(".row-centered").css({"background-color": "red"});
    }   
});
ziga1337
  • 144
  • 1
  • 10
0

You dont need to use typeof there

$(function() {
        if($("html").data('highcontrast-applied') == true) {
            $(".row-centered").css({"background-color": "red"});
        }
 });

this works like this

Tobias Fuchs
  • 910
  • 5
  • 8
0

You should use

if($("html").attr('data-highcontrast-applied')){}

or

if($("html").data('highcontrast-applied')){}
Shuaib Khan
  • 119
  • 4
0

You can use native javascript to achieve

$(document ).ready(function() {
        var temp = document.getElementsByTagName("html")[0].getAttribute("data-highcontrast-applied");
    console.log("temp==", temp);
        if(temp === 'true')
        {
            $(".row-centered").css({"background-color": "red"});
        }

    });

https://jsfiddle.net/tyfg6ezv/

MANITORATION
  • 557
  • 2
  • 5
  • 19
0

You can try with .addClass().It's working.

$(document ).ready(function() {
  var temp = document.getElementsByTagName("html")[0].getAttribute("data-highcontrast-applied");
  if(temp === 'true')
  {
   $(".row-centered").addClass("intro");
  }
 
 });
.intro{
  background-color : red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<html data-highcontrast-applied="true">
<head>

</head>
<body>
  <div class="row-centered">
    <p>
    Helllllllooooooooooo
    </p>
  </div>
</body>
</html>
Pushprajsinh Chudasama
  • 7,772
  • 4
  • 20
  • 43