0

So I have the below code that is working great for me!

$("#custom_logo").css("marginLeft", $(".rt-container:first").css("marginLeft"));

But my problem is that I want to be able to add 15 more pixels to the margin but the below code does not work at all.

$("#custom_logo").css("marginLeft", $(".rt-container:first").css("marginLeft")+15); 

I am presuming that is it because the output will be somthing like '260px15' rather than '275px' which is what I am aiming for. I was wondering if there was a way to do this simply that I am over looking?

3
  • 1
    Debug the steps. You'll see you're adding a string "15px" and a number and "15px"+15 isn't pretty. Commented May 26, 2014 at 10:48
  • dystroy, his original post includes that he realizes this is the problem :) Commented May 26, 2014 at 10:52
  • First try and print $(".rt-container:first").css("marginLeft") in console. If it works implement the below answers. Commented May 26, 2014 at 10:52

2 Answers 2

2

The following will work for you!

$("#custom_logo").css("marginLeft", parseInt($(".rt-container:first").css("marginLeft")) +15);

parseInt() can remove the 'px' from the end of the string and convert to an integer.

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

1 Comment

Thanks since I don't used much js I didn't realise paraseInt worked in that way :) Works Perfectly now!
0
$("#custom_logo").css("marginLeft",

 ($(".rt-container:first").css("marginLeft").slice(0,-2) +15 )* 1)

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.