简体   繁体   中英

Change a single value of a textbox Javascript

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM