colinbrownec
colinbrownec

Reputation: 81

Set input of type number to string

I am building my own context menu and am replacing copy / cut / paste functionality. I have copy and paste working but I cannot paste arbitrary text into an input of type 'number'.

I am setting the value using javascript similiar to below

document.getElementById('input').value = 'some text'; 

Whenever type is of number, the text is lost and the input is left blank.

Can I set the input to a string? The system paste allows this, but the input seems to parse the input and fail.

Obligatory jsfiddle

Upvotes: 0

Views: 93

Answers (1)

Bijan
Bijan

Reputation: 8586

You can change

<input id="input" type="number" value="1"></input>

to

<input id="input" type="text" value="1"></input>

Or change it in your javascript by adding

document.getElementById('input').setAttribute('type','text')

in your setText() function

jsFiddle: Here is the resulting jsFiddle

Upvotes: 2

Related Questions