0

I have script that is taking a HTMLElement and the css.top and css.marginLeft of an element which refuses to set the properties in TypeScript.

here's my code:

let moveable1: HTMLElement = document.getElementsByClassName('moveable1')[0] as HTMLElement;

Here's how I'm getting the values and "trying" to set the properties.

console.log("**style.top** = " + (moveable1.style.top = 
String((+this.chatScrollTop + +this.boxScrollTop).toFixed(0))));
console.log("**style.marginLeft** = " + (moveable1.style.marginLeft = String((+this.chatScrollLeft + +this.boxScrollLeft).toFixed(0))));

moveable1.style.top = String(moveable1.style.top);
moveable1.style.marginLeft = String(moveable1.style.marginLeft);

What's happening is:

moveable1.style.marginLeft and moveable1.style.top ALWAYS equals ""

I don't understand.

The console logs are reporting the correct values

style.top = 69
style.marginLeft = 100
top: **<<<=== "EMPTY"** and should be 69
marginLeft: **<<<=== "EMPTY"** and should be 100

Thoughts, anyone?

UPDATE:

Zeh suggested the solution:

I modified it a wee bit...

  let top = +this.chatScrollTop + +this.boxScrollTop;

  const marginLeft = this.chatScrollLeft + this.boxScrollLeft;

  moveable1.style.top = top.toFixed(0) + "px";
  moveable1.style.marginLeft = String(parseInt(marginLeft).toFixed(0)) + "px";

  console.log("top: " + moveable1.style.top);
  console.log("marginLeft: " + moveable1.style.marginLeft);

THANK YOU ZEH!

2
  • 3
    You need to add a unit to make it valid. Commented Feb 12, 2019 at 23:39
  • Are chatScrollTop, boxScrollTop, etc coming as strings? There's way too many conversions there, some of them guaranteed to be unnecessary (String(), parseInt(), etc). Commented Feb 13, 2019 at 3:11

1 Answer 1

1

You're setting a style property to a number and then trying to re-read and convert it to a string. This doesn't work; top (et al) cannot be numbers therefore they're kept at their previous value("").

Also, you need units ("px", "pt", etc) when setting a style, otherwise it won't set either, even if it's a string. Hence when you try converting them from number to string you get another blank string.

// This returns 1
console.log(document.body.style.top = 1);

// Nevertheless, it didn't work, since this returns ""
console.log(document.body.style.top);

This is not a TypeScript problem, this is a JavaScript (rather, a DOM) "problem".

My suggestion is to simplify this code. It's not just hard to read, it's doing a lot that it shouldn't be doing - unnecessary conversions, depending on assignment side effects, etc.

Something like this should work:

const top = this.chatScrollTop + this.boxScrollTop;
const marginLeft = this.chatScrollLeft + this.boxScrollLeft;

moveable1.style.top = top.toFixed(0) + "px";
moveable1.style.marginLeft = marginLeft.toFixed(0) + "px";
Sign up to request clarification or add additional context in comments.

1 Comment

Cool, happy to know it worked! And no need to update the question with answers or callouts to anyone - really, the posted answers are enough. Sometimes newer answers even become available and are a better fit. That is to say, don't be surprised if someone modifies it to be more clearly readable for other users in the future.

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.