0

I guess there is something obviously wrong with this:

var elem = document.getElementById("homepage-mobile-cat1"); 
console.log(elem);

var test1 = elem.style("margin-left");
var test2 = elem.css("margin-left");

This returns elem.style or elem.css function does not exist.

The output of console.log is

<div id="homepage-mobile-cat1" class="container homepage-mobile-cat red-background-mobile" style="margin-left: 0 !important;">
  <a href="http://www.example.co.uk/literacy/">
    <div class="CSSTable homepage-cat-text" style=""><p><span style="width:80%; display:block !important;margin:0 auto;">  Literacy Resources</span></p></div> <!-- CSSTable homepage-cat-text -->
  </a>        
</div>
3
  • You should be reading a lot about these methods and how this tasks are to be achieved.... Commented Dec 28, 2017 at 12:39
  • Use elem.style.marginLeft; and there is no method elem.css exists in javascript Commented Dec 28, 2017 at 12:41
  • you are trying to mix javascript with jquery. in javascript :- elem.style.marginLeft and in jquery it is $("#homepage-mobile-cat1").css("margin-left") Commented Dec 28, 2017 at 13:09

4 Answers 4

2

style is not a function in javascript.elem.style.marginLeft will give the value

var elem = document.getElementById("homepage-mobile-cat1");
var test1 = elem.style.marginLeft;
console.log(test1)
<div id="homepage-mobile-cat1" class="container homepage-mobile-cat red-background-mobile" style=" margin-left: 0 !important;">
  <a href="http://www.example.co.uk/literacy/">
    <div class="CSSTable homepage-cat-text" style="">
      <p><span style="width:80%; display:block !important;margin:0 auto;">  Literacy Resources</span></p>
    </div>
    <!-- CSSTable homepage-cat-text -->
  </a>
  </div>

css is a jquery method & it will work with jquery object

console.log($("#homepage-mobile-cat1").css("margin-left"))
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="homepage-mobile-cat1" class="container homepage-mobile-cat red-background-mobile" style="margin-left: 0 !important;">
  <a href="http://www.example.co.uk/literacy/">
    <div class="CSSTable homepage-cat-text" style="">
      <p><span style="width:80%; display:block !important;margin:0 auto;">  Literacy Resources</span></p>
    </div>
    <!-- CSSTable homepage-cat-text -->
  </a>
  </div>

Sign up to request clarification or add additional context in comments.

Comments

1

Try jquery:

alert($('#homepage-mobile-cat1').css('margin-left'));

Comments

0

var elem = document.getElementById("homepage-mobile-cat1"); 


// You can use this method to calculate the style
var test1 = window.getComputedStyle(elem).getPropertyValue("margin-left");
console.log(test1);


//Or directly get the value from style. But this requires the property value to be already assigned to the style. If not, use the method above.
console.log(elem.style.marginLeft);

//Or alternatively, you can use JQuery
console.log($("#homepage-mobile-cat1").css("margin-left"));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="homepage-mobile-cat1" class="container homepage-mobile-cat red-background-mobile" style="



     margin-left: 0 !important;"><a href="http://www.example.co.uk/literacy/">
      <div class="CSSTable homepage-cat-text" style=""><p><span style="width:80%; display:block !important;margin:0 auto;">  Literacy Resources</span></p></div> <!-- CSSTable homepage-cat-text --></a>



      </div>

Comments

0

Try elem.style.marginLeft.

var elem = document.getElementById("homepage-mobile-cat1"); 
console.log(elem);

var test1 = elem.style.marginLeft;
console.log(test1);
<div id="homepage-mobile-cat1" class="container homepage-mobile-cat red-background-mobile" style="margin-left: 0 !important;">
  <a href="http://www.example.co.uk/literacy/">
    <div class="CSSTable homepage-cat-text" style=""><p><span style="width:80%; display:block !important;margin:0 auto;">  Literacy Resources</span></p></div> 
  <!-- CSSTable homepage-cat-text -->
  </a>

</div>

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.