0

I'm trying to get the name input element by id using "name" and print it on the console:

var name = document.getElementById("name");
console.log(name);
var surname = document.getElementById("surname");
console.log(surname);

var select = document.querySelector("select");

console.log(select);

select.addEventListener("mouseleave", function(){
    console.log(select);
    var optionSelected = select.value;
    console.log(optionSelected);
    if (optionSelected === "beginner"){
        alert("it works!!!");

    }

})

but I get this on the first console.log:

[object HTMLInputElement]

As soon as I rename var name into var obj and do console.log(obj), i get the correct element printed on the console:

<input type="text" id="name" value="hi">

This behaviour is quite strange because the surname input doesn't give any problem.

I'm using Visual Studio Code and Google Chrome.

JackieNBee
  • 173
  • 3
  • 18
  • @deceze I think that's the wrong dupe, it's more like [What do querySelectorAll and getElementsBy* methods return?](https://stackoverflow.com/questions/10693845/what-do-queryselectorall-and-getelementsby-methods-return). Although there is `getElementById` too. – VLAZ Dec 10 '19 at 14:16
  • @VLAZ Nope, it's precisely *that* issue. – deceze Dec 10 '19 at 14:17
  • @VLAZ even I thought the same but marked dupe is correct. Refer this line: *As soon as I rename var name into var obj and do console.log(obj)* – Rajesh Dec 10 '19 at 14:17
  • 1
    @deceze You're right I...failed to read onwards. To exactly the sentence Rajesh pointed out. I'd leave the link (and my *shame*) here just for reference. – VLAZ Dec 10 '19 at 14:18

0 Answers0