2

How to remove a property from CSS Style using JQuery or Javascript. I want to remove the width property from the below div style. I tried working with the link but it did not help even. The property that i want to remove from style is a div that has no ID associated but has only classname.

<div class="floatHeads" style="left: 0px; top: 59.34px; width: 443px; overflow: hidden; padding-right: 0px; padding-left: 0px; margin-top: 0px; position: absolute; z-index: 1001;">

I tried doing the below:

.floatHe .floatHeads {
    width: '';
}

also tried below

$('.floatHeads').css({width: ''});

Nothing from the above worked for me. Please guide.

Community
  • 1
  • 1
sTg
  • 4,313
  • 16
  • 68
  • 115

5 Answers5

3

function removeWidth(){
 $(".floatHeads").css("width", "")
}

function addWidth(){
 $(".floatHeads").css("width", "400")
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="floatHeads" style="left: 0px; top: 59.34px; width: 443px; overflow: hidden; padding-right: 0px; padding-left: 0px; margin-top: 0px; position: absolute; z-index: 1001; border: 1px solid black;"></div>

<button onclick=removeWidth()>
Remove width
</button>

<button onclick=addWidth()>
Add width
</button>
Flying Gambit
  • 1,238
  • 1
  • 15
  • 32
0

You might need to wrap your code in a handler for ready() so that it runs after the document loads?

$(document).ready(function() {
    $('.floatHeads').css({width: ''});
});
Matt
  • 3,677
  • 1
  • 14
  • 24
0

With plain Javascript

var x = document.querySelectorAll(".floatHeads");

for (var j = 0; j < x.length; j++) {
        console.log(x[j].style.width);
    }
var i;
    for (i = 0; i < x.length; i++) {
        x[i].style.width="";
    }

for (i = 0; i < x.length; i++) {
        console.log(x[i].style.width);
    }
<div class="floatHeads" style="width:10px" >
  </div>
WitVault
  • 23,445
  • 19
  • 103
  • 133
0

You can use "unset" to "reset" to inherited value. MDN page on unset here.

$('.floatHeads').css({width: 'unset'})

0

Here is one more way to look how it works:

$(".rezult").html($(".floatHeads").attr("style"));

function myfunc(){

   $(".floatHeads").css("width", "");

   $(".rezult").html($(".floatHeads").attr("style"));
}
.floatHeads {
    background: orange;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="floatHeads" style="left: 0px; top: 59.34px; width: 443px; overflow: hidden; padding-right: 0px; padding-left: 0px; margin-top: 0px; position: absolute; z-index: 1001;">ewewew</div>
<button onclick="myfunc()" value="click me">remove width</button>
<div class="rezult"></div>
Banzay
  • 9,310
  • 2
  • 27
  • 46