Need to take variables from user input (Javascript ) [closed]

get the input from the input element and split the input element value using string.split() method. It will separate it in array. So, using array index, you could get those array values in separate variable.

declare those variables globally to get that value from anywhere and any other js file you want. I created a simple demo for 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 THIS DEMO

In this demo I just alert those variable values.

Leave a Comment