How to translate a value from inputs into a js file variable?

  • 0
    I have a google extension, popup.html has three inputs and a button
    <p class="in">Name</p>
       <input type="text" id="name">
       <p class="in">Last name</p>
       <input type="text" id="lastname">
       <p class="in">Link</p>
       <input type="text" id="link">
       <button type="submit" id="save">Save</button>

    I enter data into these inputs, when the button is clicked, I need to save the values ​​to variables and run the js code on the site with these variables. How to implement this? Thank you in advance.
    JavaScript Anonymous, Sep 26, 2020

  • 4 Answers
  • 0
    Elements of this type have a field value

    That is, you store the value of this field in some variable (or immediately substitute it somewhere without memory allocation):

    const inputValue = document.getElementById("something").value;

    If you are interested in what other fields such elements have, then you can simply output to the console the variable that stores this element (again, you do not need to create a variable, you can immediately substitute getElement ... () as an argument, but as for me, it hits the code readability):

    const input = document.getElementById("something");


  • 0
    Hang the click event on the button. Actually, then you just pull out the field values.


    Getting data from input
    Logan Tate

  • 0
    <button type="submit" id="save" onclick="func1()">Save</button>

    const func1 = () => {
    const name = document.getElementById("name").value;
    const lastname = document.getElementById("lastname").value;
    const link = document.getElementById("link").value;
    console.log(`Name: ${name}\nLast Name: ${lastname}\n Link: ${link}`)
    Hadley Henry

  • 0
    Andrew Lester

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!