0

I want to use the same input area as type: text or type: URL at the same time; Is there any way to that

<form onSubmit={this.onFormSubmit}>
     <div className='form'>
         <input
             type='text'
             placeholder='Enter search keyword or image URL'
             value={this.state.searchTerm}
             onChange={(e) => {
                 this.setState({
                     searchTerm: e.target.value
                 })
                 console.log(e.target.type)
             }} />
         <button type='submit'>Display</button>
    </div>
</form>

The input area is for image search. And I want the user to either use a keyword or image url. I'm using unsplash API for image search

1

1 Answer 1

2

There is no way to do multiple types, but you have the ability to swap the type. Basic idea below

document.getElementById("testNum").addEventListener("input", function () {
  this.type = this.value.match(/^[\d.]+$/) ? 'number' : 'text'
})

document.getElementById("testLink").addEventListener("input", function () {
  this.type = this.value.match(/^http/i) ? 'url' : 'text'
})
input[type="text"] {
  border-color: green;
}
input[type="number"] {
  border-color: yellow;
}
input[type="url"] {
  border-color: orange;
}
<input id="testNum" type="text" />

<input id="testLink" type="text" />

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

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.