How to use document.querySelector() in javascript | Community
Skip to main content
Level 3
April 20, 2021
Solved

How to use document.querySelector() in javascript

  • April 20, 2021
  • 2 replies
  • 6491 views

I'm trying to use document.querySelector() in javascript.

There is a element in my html with 'ul' tag.

then in my javascript I'm using it as :

const ultag = document.querySelector("ul");

then I'm calling a function for pagination like :

function x(pagecount,currentpage)

{

let litag =' ';

if (page<1)

{

litag= something;

}

ultag.innerHTMl = litag;

}

but it is showing error in ultag.innerHTML as it is showing null value for ultag.

Please help how to use it.

 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Asutosh_Jena_

Hi @shagunmalik 

 

Let's say you have the HTML markup as:

 

<ul class="project-list single-col">
<li class="project-item" style="clear: left;">
Something
</li>
<li class="project-item" style="clear: left;">
Something
</li>
<li class="project-item" style="clear: left;">
Something
</li>
</ul>

 

In JS If you use the below code:

var x = document.querySelector("ul");

 x will hold the complete markup that we have above.

 

Thanks!

2 replies

Asutosh_Jena_
Community Advisor
Asutosh_Jena_Community AdvisorAccepted solution
Community Advisor
April 20, 2021

Hi @shagunmalik 

 

Let's say you have the HTML markup as:

 

<ul class="project-list single-col">
<li class="project-item" style="clear: left;">
Something
</li>
<li class="project-item" style="clear: left;">
Something
</li>
<li class="project-item" style="clear: left;">
Something
</li>
</ul>

 

In JS If you use the below code:

var x = document.querySelector("ul");

 x will hold the complete markup that we have above.

 

Thanks!

Level 3
April 20, 2021
it is showing null value for x.
Anudeep_Garnepudi
Community Advisor
Community Advisor
April 20, 2021

@shagunmalik 

Are you sure you have at-least one ul tag in page? If there is no ul tag in DOM, document.querySelector("..") will return null.

AG