0

I'm working in ORMB and have an input element like this

<input id="charVal" class="oraInput" oraField="charVal">

I want to dynamically add an oraSearch attribute using Javascript but it's not working

document.getElementById("charVal").setAttribute("oraSearch","CM_SR_CHAR");

Though if I try with other attributes, it's working fine. Also if I add the oraSearch statically like below, then also it's working

<input id="charVal" class="oraInput" oraField="charVal" oraSearch="CM_SR_CHAR">
halfer
  • 18,701
  • 13
  • 79
  • 158
Vishal Afre
  • 845
  • 3
  • 11
  • 35
  • There is no such attribute in HTML. I assume it is an expando attribute which has failed to use the data- prefix and is being read by JavaScript. You need to run your JavaScript **before** the JavaScript that looks for it. – Quentin Jun 27 '16 at 10:50
  • run which JS before which JS?? – Vishal Afre Jun 28 '16 at 07:36
  • "Your JS" = The JS you put in your question. "the JavaScript that looks for it" = whatever JavaScript is also in the page that reads that attribute, I don't know what JavaScript that is, but it must exist because nothing else is going to do anything with the attribute. – Quentin Jun 28 '16 at 08:43
  • … unless the non-standard attributes are processed server side, in which case JS can't solve the problem at all. – Quentin Jun 28 '16 at 09:05

1 Answers1

0

I believe the problem is in the DOM path for the JavaScript code.

I tried to put the script from the <head> and it didn't work, like this:

<!DOCTYPE html>
<html>
<head>
<script>
document.getElementById("charVal").setAttribute("oraSearch","CM_SR_CHAR");
</script>

</head>
<body>

<input id="charVal" class="oraInput">


</body>
</html>

But, if you put it in the body, it works fine:

<!DOCTYPE html>
<html>
<head>


</head>
<body>

<input id="charVal" class="oraInput">
<script>
document.getElementById("charVal").setAttribute("oraSearch","CM_SR_CHAR");
console.log(document.getElementById("charVal"));
</script>

</body>
</html>

Note: if you are taken the script from another file put the <script></script> inside the <body>

tomer raitz
  • 370
  • 2
  • 10