Query Selector not appearing in console

<div class="output"></div>
<input type="number" value="100">
<button type="button">Tip 1</button>
<button type="button">Tip 2</button>

However when I type this in the console:

var temp = document.querySelector("button") it should return button type, but I am getting an error(undefined).

what’s wrong. Is my console an issue?

That’s strange, for I cannot recreate your problem on my end.

Can you please upload the contents of your html file to somewhere like jsbin.com for us to further investigate?

1 Like

Hi there @Paul_Wilkins,

I have no issues in posting in JSBIN, but the code is pretty simple I am directly giving you the HTML file. I have scanned it so you can download and open in browser w/o facing any virus issues.

Rest what I was trying can be done in browser console. thanks.

3-lesson.html (606 Bytes)

Well that works for me, so it might be the web browser that needs investigating.

Which browser and version are you using?

1 Like

Oh hang on - I think I know what might be the problem.

Is it the undefined in the console that you’re asking about?

1 Like

If you check the temp variable you’ll see that it’s correctly assigned the button to the temp variable.

Where the undefined is coming from is the var statement itself. The variable temp is correctly assigned, and the var statement itself doesn’t return any value which is why you’re shown undefined. There’s nothing bad about that.

2 Likes

Hi there,

I am using chrome.
Google Chrome is up to date
Version 74.0.3729.169 (Official Build) (64-bit)

Yes. That’s right.

JS is interpretable script. If you create var temp just before [button], it found no buttons.

That’s an incorrect assumption.

The undefined has nothing to do with the button, and is instead due to var statements having no return value.

You’ll find that exactly the same undefined occurs when you assign any variable in the console.

> var temp = "a string";
< undefined
> temp
< "a string"

And for the language nerds, that makes sense.

Statements return values so that their results can be interpreted.
A variable definition can never fail; it can receive ‘bad’ input, but it can never fail itself given legitimate (or interperably legitimate) values.
Thus the language construct of a variable definition has no return type - the statement will always succeed, and the results can be checked by inspecting the already defined variable.

2 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.