Change a single value of a textbox Javascript

604 views Asked by At

I want to change only one value of an input, example: in the input income "xy" I want to change is only "and" Suppose a new value is "p", and "x" leave. The end result should be "xp".

http://s2.subirimagenes.com/privadas/previo/thump_2265651input.png http://s2.subirimagenes.com/privadas/previo/thump_2265653input2.png

<input type="text"id="txtChar1" onkeypress="isNumberKey();">

function isNumberKey()
{
  var text = document.getElementById(txtChar1).value;

  if (text[1] >= 0)
  {
    text[1].write("p");
    text[1].value ="p";
    text.charAt(1).value = "p";
    text[1].innerHTML = "p";
    text[1] = "p";
  }
}

Forget clarify the options that exist in this condition are those that probe and did not walk any.

1

There are 1 answers

8
six fingered man On

Strings are immutable. Your description is unclear, but the best I can tell is that you want to reverse the order of the characters.

function isNumberKey()
{
  var el = document.getElementById("txtChar1");
  el.value = el.value.split("").reverse().join("");
}

The .split("") converts the string into an Array of its characters. Then .reverse() reverses the order of those Array members and .join("") joins the members back into a string.


Another approach would be to pass this to the function, since that's the one getting updated.

<input type="text"id="txtChar1" onkeypress="isNumberKey(this);">

function isNumberKey(el)
{
  el.value = el.value.split("").reverse().join("");
}

I won't go into detail about what was wrong with your code, because I think you were just throwing together a bunch of arbitrary bits of code in the hope that it would work. Needless to say, it's almost all wrong.