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>
  <meta charset="utf-8">
  <title>JS Bin</title>
<input type="text" id="gettxt" />
<input type = "button" onclick="callfun()" value="click me!!"/>

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];


In this demo I just alert those variable values.

Browse More Popular Posts

Leave a Comment