-1

hi i have javascript issue that i wrote a input when i change it text to sadra it doesn't alert "your text contains "sadra" word " how can i resolve it ? thanks for helping ...

here is the code:

var realText;

function text(realText) {
  var realText = document.getElementById('input1').innerHTML;
  if (realText.includes('sadra')) {
    alert("your text contains \"sadra\"  word ");
  }
}
<label>Input :</label>
<input type="text" name="test" id="input1" onchange="text(realText)" />
IR Fifa
  • 35
  • 4
  • 1
    input's don't have `innerHTML` they have a `value` – Bravo Sep 07 '21 at 07:38
  • You are overriding a function parameter within the function itself, which is bad practice. If you are getting the value of the input field with a selector you don't need the parameter here at all – empiric Sep 07 '21 at 07:39
  • 1
    There's also no point in that global `var realText`, the `realText` function parameter or passing `realText` in `onchange`. You can remove all those. – deceze Sep 07 '21 at 07:39

1 Answers1

2

If you are trying to access the value inside the element with id input1, then the selector should point to value of the node.

document.getElementById('input1').value

Also there is no need to pass the realText parameter to the onchange event. This is not going to have an impact on the functionality.

innerHTML gives the html code inside a specified id

innerHTML Example

function text() {
  var realText = document.getElementById('container').innerHTML;
  console.log('Inner HTML', realText);
}
<div id="container">
  <label>Input :</label>
  <input type="text" name="test" id="input1" onchange="text()" />
</div>

Your Working fiddle

function text() {
  var realText = document.getElementById('input1').value;
  if (realText.includes('sadra')) {
    alert("your text contains \"sadra\"  word ");
  }
}
<label>Input :</label>
<input type="text" name="test" id="input1" onchange="text()" />
Nitheesh
  • 19,238
  • 3
  • 22
  • 49