Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

107
Views
Select/edit all children elements inside a div

How can I use jquery or css to assign a .class to all elements inside a div?

i.e this is what I have

<div class="nav">
   <a href="">one</a>
   <a href="">two</a>
</div>

But this is what I want

<div class="nav">
   <a href="" class="test">one</a>
   <a href="" class="test">two</a>
</div>

Using as little code as possible, so that when the page loads, the elements in .nav have the class "test" assigned to them and I don't have to assign it in every single

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You can achieve this by following code

<html>
    <head>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    </head>
    <body>
        <div>
            <a href="">one</a>
            <a href="">two</a>
        </div>
    </body>
    <script type="text/javascript">
        $(document).ready(function () {
            $('div > a').addClass('test')
        })
    </script>
</html>
about 4 years ago · Juan Pablo Isaza Report

0

Pretty simple in jQuery: $('div a').addClass('class');

Or in JavaScript something like this:

let links = document.querySelectorAll("div a");

for(let link of links) {
  link.classList.add("class");
}
.class {
  text-decoration: none;
  color: orange;
  font-size: 50px;
  margin-right: 100px;
}
<div class="nav">
   <a href="">one</a>
   <a href="">two</a>
</div>

about 4 years ago · Juan Pablo Isaza Report

0

$(document).ready(function() { $('.nav a').each(function(){ $(this).addClass("Test"); }) });
.Test{
color:red
}
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    
    <div class="nav">
        <a href="">one</a>
        <a href="">two</a>
    </div>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!