0

I have a simple Javascript function, but its not working inside a form. It looks like this:

echo "<tr><td><form name='formfolder' method='POST' action='?module=pindah_dok_index' >";
echo "<input type=hidden name='id_debt' value='$_GET[id_debt]' />";
echo "move docs</td><td>";
echo "<input type='text' name='index1_dok' id='xcvbn' onkeyup='xcvbn()' style='width:80px;' required />";

But when I place my input #xcvbn before / outside the form, it works perfectly.

My Javascript function is simple like this:

function xcvbn(){    
  var xx=document.getElementById("xcvbn");
  var x = xx.value;
  var str = x.replace(/[^0-9a-zA-Z\-_.]/g, ''); 
  var str = str.toUpperCase();
  xx.value = str;
}
3
  • what's the error you are getting? Commented Feb 7, 2018 at 6:56
  • we need to see the whole form, and to also know what errors you receive (perhaps in the console) or what you mean by its not working Commented Feb 7, 2018 at 6:56
  • Any error on console?? Commented Feb 7, 2018 at 6:58

1 Answer 1

2

Your input element's id is same as the function name.

Some browsers directly accept the form element's id in JS. See the below snippet (tested in FF).

console.log(asdf.value);
<form>
<input id="asdf" value="text" />
</form>

So it makes a conflict. That's why function was not triggered. use different name for input elements id and function.

Better you can pass this object in the function argument like below.

function xcvbn(elem) {
  var x = elem.value;
  var str = x.replace(/[^0-9a-zA-Z\-_.]/g, '');
  var str = str.toUpperCase();
  elem.value = str;
}
<tr>
  <td>
    <form name='formfolder' method='POST' action='?module=pindah_dok_index'>
      <input type=hidden name='id_debt' value='$_GET[id_debt]' />move docs</td>
  <td>
    <input type='text' name='index1_dok' id='elemId' onkeyup='xcvbn(this)' style='width:80px;' required />

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

5 Comments

I doubt that's the issue. That would be a real problem - if the function names would be mixed with the DOM ids
But you are right. Didn't know there's a conflict between the two. +1
@Adelin Some browsers directly accept the form element's id in JS. So it makes conflicts. I would add this info in my post.
wow its workkkk. thank you so much mr @sankar, really appreciate that. thanks everyone. thanks from me newbie :D

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.