0

After trying multiple ways of what I want to do, which all failed, I'm asking here. This is probably pretty basic, but I just can't do it.

What I essentially want to do:

  1. Create a variable
  2. "Assign" a text box (value) to it
  3. Automatically have the variable's content change to whatever is put into the text box
  4. Potentially have the variable's value used somewhere else immediately

If the user had to press a button to update the element using the variable's value, that'd be OK, too, I just want to have this done.

4
  • So, on input change - you want to assign this value to a variable. :) Commented Dec 27, 2017 at 20:38
  • Post your version of the code Commented Dec 27, 2017 at 20:38
  • Are you using jQuery? Post your current code, so we can help Commented Dec 27, 2017 at 20:39
  • 1. I don't have any more of the code. Since none of my tries worked, I didn't keep any of the files. 2. I'm not using jQuery. @tymeJV - I have no idea what that means. I'm not that much of a geek about JS. I understand more of HTML. Commented Dec 27, 2017 at 20:43

3 Answers 3

1

Alright, I have to correct myself. Another try worked, with the result of 'undefined'.

<head>
    <meta id="test3" charset="utf-8">
    <link rel="stylesheet" href=".\css\Starter.css">
    <title id="test1">TITEL</title>
    <script>
        function txtSet(txtInp) {
            var txt = txtInp.value
            document.getElementById('txtP').innerHTML = txt
            }
    </script>
</head>

<body>
    <input type="text" id="txtInp" onkeyup="txtSet(txtInp.value)"></input>
    <p id="txtP"></p>
</body>
Sign up to request clarification or add additional context in comments.

Comments

0

Try this one:

    <body>

    <script>
    var a = "";
    function changeVariable(){
         document.getElementById('demo2').value=document.getElementById('demo').value;
    }
    </script>

    <input type="text" id="demo" onkeyup="changeVariable()"></input>
    <input type="text" id="demo2"></input>

    </body>

Comments

0

I think that you're searching for onkeyup if it's so: you can use as it follows:

In your html

<input type="text" name="name" id="id" onkeyup="yourFunction(this.value);">

and in your js file

var theVariable;
function yourFunction(theTextInTheTextBox){
theVariable = theTextInTheTextBox;
}

It could also be onkeypress or onkeydown events, just try the three to see which is the one that you're actually searching for. To see the difference between the three I advise you to take a look at this link

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.