3

I want to call javascript function in left prop from style inside div tag.

function setLeft() {
        return '15px';
    }
<div class="configValue" style="position: absolute; left: setLeft()">
  <strong>
   @Html.DisplayNameFor(m => m.First().ConfigurationValue)
  </strong>
</div>

I tried also something like this, but did not work:

"javascript:setLeft();"
Sparky
  • 287
  • 1
  • 11
gorrch
  • 521
  • 3
  • 16
  • 3
    you cannot do this, but you can change left prop by script in js – Slavik Sep 13 '18 at 12:48
  • You can't. However you *can* invoke a function after the page loads and [edit the style](https://stackoverflow.com/questions/14753147/add-inline-style-using-javascript) there. – BackSlash Sep 13 '18 at 12:49
  • A way of doing this would be: 1. `style="position: absolute; left:var(--left)"` and 2. use a javascript function to set the `--left`variable: `yourElement.style.setProperty("--left", "15px"); – enxaneta Sep 13 '18 at 13:23

2 Answers2

1

You cannot do this. However, you could change the style from JS though.

var configValue = document.querySelector('.configValue');
configValue.style.left = setLeft();
evolutionxbox
  • 3,932
  • 6
  • 34
  • 51
Ozzy Walsh
  • 877
  • 9
  • 17
-1

Why don't you just use jQuery to add an attribute?

<div class="configValue" style="position: absolute" onload="setleft()">

jQuery:

function setleft(){
    $(this).css(left:"15px");
}

This should work. Maybe my syntax is wrong though, haven't done web development in a while

S ProaBusi
  • 59
  • 5