0
function ftest (P1, P2) {
"use strict";

  document.getElementById(String(P1)).display = String(P2);
}

document.getElementById(String(P1))
use the parameter P1 as the ObjectID;

display = String(P2)
use parameter P2 as the ObjectStyle Property;

this way manner to avoid clutter when passing values at the workflow::

onclick="ftest(objectname,objectproperty)"

instead of

onclick="ftest('objectname','objectproperty')"

the result would be Object is Null or Unknown Property ?
when trying to make things clean at the html code;

H3sDW11e
  • 188
  • 1
  • 3
  • 11
  • No, putting `String()` around `P2` does not change the `objectname` variable that is supplied as an argument to become a `"objectname"` string literal. – Bergi Sep 23 '18 at 11:16
  • If you are looking for clean html code, just [don't use `onclick` attributes at all](https://stackoverflow.com/q/6941483/1048572). – Bergi Sep 23 '18 at 11:17

3 Answers3

1

You cant. The only way to do this would be:

 var objectname = "objectname";

then

 ftest(objectname)

would work. however if your aim is

to make things clean at the html code

then remove the inline eventlisteners and do something like:

 // html
 <button id="ftest" > ftest </ button>

 // js
 const on = (selector, event, handler) => document.querySelector(selector).addEventListener(event, handler);

 on("#ftest", "click", () => ftest("objectname", "objectproperty"));
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
  • understood, it is not because the onclick is typed as html string that the javascript parameters are identified as such; would have to specify it like that; – H3sDW11e Sep 23 '18 at 10:50
0

It is possible but objectproperty should be defined.

Something like that:

Somewehre in the script (quick and dirty solution):

window.objectproperty = "none";

call:

onclick="ftest(objectId, objectproperty)"

ftest:

function ftest (P1, P2) {
  "use strict";
  P1.style.display = P2;
}
brandt.codes
  • 923
  • 2
  • 9
  • 19
  • understood, it is not because the onclick is typed as html string that the javascript parameters are identified as such; would have to specify it like that; – H3sDW11e Sep 23 '18 at 10:50
0

You must create object then pass it to function.

look at this example :

function ftest (obj) {
"use strict";
  document.getElementById(obj.P1).style.display = obj.P2;
}
<h1 id='test'>I Am Block</h1>
<script>
var obj = {
 P1 : 'test',
 P2 : 'inline'
}
</script>
<button onclick="ftest(obj)">Test Me</button>
Arash Hasanzade
  • 490
  • 3
  • 12