Return result in an input

Asked

Viewed 54 times

1

Good afternoon, I need to return a result in an input. how do?

HTML

<div class="form-group">
  <input class="form-control input-lg" type="text" name="cc-number" id="cc-number">
</div>
<div class="form-group">
  <input class="form-control input-lg" name="cc-flag" id="cc-flag" type="text">
</div>

I’ll type in the input cc-number and give me the result on cc-flag

JAVASCRIPT

var input = document.querySelector('input.cc-number')
var result = document.querySelector('input.cc-flag')

input.addEventListener('change', onInputChange)
input.addEventListener('input', onInputChange)

function onInputChange (e) {
  var cards = CardType.cardType(e.target.value)
  result.innerHTML = JSON.stringify(cards)
}

OBS: It works properly by returning to a DIV. But I need it back to an INPUT

  • You want to put the value of a input within another input?

  • That’s right... the first input does a search in a file . js and returns a value. need that this value be returned inside the input

1 answer

1


The estate innerHTML is to modify the html inside a tag. The value of a input gets into value and not in innerHTML, That’s why you can’t change.

Another thing, you have no classes .cc-number and .cc-flag, but ids. So, on your selector, you should put # in place of ..

Follow the example:

var input = document.querySelector('input#cc-number')
var result = document.querySelector('input#cc-flag')

input.addEventListener('change', onInputChange)
input.addEventListener('input', onInputChange)

function onInputChange (e) {
  var cards = CardType.cardType(e.target.value)
  result.value = cards
}
<div class="form-group">
  <input class="form-control input-lg" type="text" name="cc-number" id="cc-number">
</div>
<div class="form-group">
  <input class="form-control input-lg" name="cc-flag" id="cc-flag" type="text">
</div>

<script src="https://polvo-labs.github.io/card-type/dist/card-type.js"></script>

  • I don’t want to duplicate the value in another input... I want the result to appear in the input

  • I am using this example: https://polvo-labs.github.io/card-type/example/ . In this example it returns inside a div. I need you to return inside input like I said above

  • Now I understand. I modified the example to do what you want.

  • It did work! Perfectly....

  • Does the search for 4 or 55, for example. Some searches do not return result, because the card number needs to be valid. When there is no result, the input empty.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.