0

var name = document.querySelectorAll('.info input[type=text]');
var name1 = document.querySelectorAll('.info input[type=text]');

console.log(name);
console.log(name1);
            <div class='info'>
                <div>
                    <div><label id="dd">Name</label><input type='text'></div>
                    <div><label>Father Name</label><input type='text'></div>
                
                </div>
            </div>

javascript is producing different results for same code, only variable name is different. this is the output produced on my device

3
  • name and name1 variables are referencing the same dom elements. You could simply give both of your input's a unique id and use that get their reference. Commented Aug 25, 2018 at 15:57
  • Something is odd here, your log of name1 shows 4 inputs? Commented Aug 25, 2018 at 15:59
  • You can add reference by adding separate id's for each input box. Now you are referencing same elements for 'name' and 'name1' Commented Aug 25, 2018 at 15:59

3 Answers 3

4

There is a property called name on the window object that you are printing out:

var name2 = document.querySelectorAll('.info input[type=text]');
var name1 = document.querySelectorAll('.info input[type=text]');

console.log(name === window.name);
console.log(name2);
console.log(name1);
            <div class='info'>
                <div>
                    <div><label id="dd">Name</label><input type='text'></div>
                    <div><label>Father Name</label><input type='text'></div>
                
                </div>
            </div>

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

Comments

2

The problem has to do with window.name : https://developer.mozilla.org/en-US/docs/Web/API/Window/name

As you're in the top scope, you are modifying window.name instead of creating a new var. window.name is being converted to a string, that's why it logs something different. You need to wrap that code in a function or use a different variable name.

Comments

1
let [ name1, name2 ] = document.querySelectorAll('.info input[type=text]');

<div class='info'>
   <div>
     <div><label id="dd">Name</label><input type='text'></div>
     <div><label>Father Name</label><input type='text'></div>       
   /div>
</div>

Try using a variable instead of name since that might conflict with window.name, this will only conflict if you are using this variable in your root scope. Use a modular approach instead. To avoid errors like these.

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.