DavidM
DavidM

Reputation: 57

Javascript Matching Input Value to ID

So I've been trying to assign the value a user inputs to the ID of an element. So basically if the ID is "A" and the user types "A" then the text will turn green otherwise It'll turn red. I'm not quite sure of what the syntax would be and I would greatly appreciate any help.

Fiddle: https://jsfiddle.net/swzzd5bf/2/

function search(ele) {
            if(event.keyCode == 13) {
                if(ele.value.toUpperCase() == "B"){ //this is where I am unsure
                    ele.style.color = "green";
                }
                else{
                    ele.style.color = "red";
                }
            }
        }   
<input id="b" class="box" type="text" onkeydown="search(this)">

Upvotes: 0

Views: 36

Answers (1)

Nawed Khan
Nawed Khan

Reputation: 4391

You need to compare the value of element with the ID of element like this

if(ele.value.toUpperCase() == ele.id.toUpperCase())

Here is the updated fiddle

Upvotes: 2

Related Questions