How to use document.querySelector() in javascript | Community
Skip to main content
Level 3
Apr 20, 2021 12:02 PM UTC
Solved

How to use document.querySelector() in javascript

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.
Asutosh_Jena_
Asutosh_Jena_Accepted solution
New Participant
Apr 20, 2021 12:08 PM UTC

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!

16 replies

Anudeep_Garnepudi
New Participant
Apr 20, 2021 3:52 PM UTC

@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.

Translate
Asutosh_Jena_
Asutosh_Jena_Accepted solution
New Participant
Apr 20, 2021 12:08 PM UTC

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!

Translate
Level 3
Apr 20, 2021 12:11 PM UTC
it is showing null value for x.
Translate