0

For example, if i had a form like this:

<form id="form1">
  <input type="text" name="item">
</form>
<button onclick="outputItem"></button>

How would I output the input from the form?

8
  • What do you want to achieve from output? Commented Dec 18, 2018 at 7:51
  • How would I out put the input form the form bit more clear? Commented Dec 18, 2018 at 7:52
  • 1
    you can use javascript for that -> var input = document.getElementByName('item'); Commented Dec 18, 2018 at 7:52
  • I want to output the value of the input with the name "item" Commented Dec 18, 2018 at 7:53
  • I tried using document.getElementNyName('item').value and it returned as undefined Commented Dec 18, 2018 at 7:55

4 Answers 4

1

You can write a function in which you can use querySelector() to get the element to get the value of the input:

function outputItem(){
  var v = document.querySelector('#form1 > input[name=item]').value;
  console.log(v);
}
<form id="form1">
  <input type="text" name="item">
</form>
<button onclick="outputItem()">Print</button>

Sign up to request clarification or add additional context in comments.

Comments

0
<form id="form1">
  <input type="text" name="item" id="item">
</form>
<button onclick="outputItem()">ShowMe</button>
<span id="response"></span>

<script>
    function outputItem() {
        var item = document.getElementById('item').value;
        document.getElementById('response').innerHTML = item;
    }
</script>

Try above one.

Comments

0

You can do like:

function selectInputForm(){
const selectForm = document.querySelector('#inputForm');
}

Remember to add the id to the input form you want to select:

<form id="form1">
  <input type="text" name="item" id= 'inputForm' >
</form>
<button onclick="selectInputForm()"></button>

Comments

0

Before clicking on the button Try it you have write some text into the input. Otherwise, it can get an error - undefined.

function outputItem() {
  var x = document.getElementsByName("item")[0].value;
  document.getElementById("demo").innerHTML = x;
}
<form id="form1">
  <input type="text" name="item">
</form>

<button onclick="outputItem()">Try it</button>

<p id="demo"></p>

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.