Richard
Richard

Reputation: 1167

Why won't the attribute of an input field in javascript change after onchange?

I tried looking around for an answer on here but couldn't find anything that resembles this problem, but what I am trying to do is get an input cell to format itself after a user inputs a number i.e. "100" will format to "100%" but that never happens.

When the onchange event fires it works and processes the input just fine but when it comes time to change the attribute "value" of the input field nothing happens. Why is this?

In my javascript I have

<input onchange='update();' ....

The update method is called but when it comes to this line

element.setAttribute(attr, value);

Nothing seems to happen. Does the focus of the input box need to be removed before changing the value of the input box?

Upvotes: 0

Views: 313

Answers (2)

RichardB
RichardB

Reputation: 2767

Try

<input onchange='update(this);'

and

function update(element) {
element.value=value;

You don't seem to be telling it which element is being referred to (unless you call it elsewhere in your function).

Upvotes: 0

tymeJV
tymeJV

Reputation: 104775

Just call .value

element.value = value;

Upvotes: 1

Related Questions