Need to take variables from user input (Javascript ) -
if user gives input this: xxx yy zz. need , detect xxx variable , take yy xxx1(variable1) & zz xxx2(variable2).the values of xxx1 , xxx2 used in someother files.
this code using:
var str = prompt("enter values") ; var array = str.split(' '); var x = array[0], x1 = array[1], x2 = array[2];
but if input given like:
stack 12 3
overflow 13 4
i need 12 stack1 ,3 stack2 , 13 overflow1 , 4 overflow2. hope information enough.
i new javascript, detailed description helpful.
get input input element , split input element value using string.split() method. separate in array. so, using array index, array values in separate variable.
declare variables globally value anywhere , other js file want. created simple demo you...
html markup
<!doctype html> <html> <head> <meta charset="utf-8"> <title>js bin</title> </head> <body> <input type="text" id="gettxt" /> <input type = "button" onclick="callfun()" value="click me!!"/> </body> </html>
javascript code
var first; var second; var third; function callfun() { var gettxt = document.getelementbyid('gettxt'); var array = gettxt.value.split(' '); first = array[0], second = array[1], third = array[2]; alert(first); alert(second); alert(third); }
see demo
in demo alert variable values.
Comments
Post a Comment