1

I want to apply JavaScript on sticky header in my site. For this I want to target a div inside sticky div by JavaScript. Please let me know how to target a dive that is inside some other divs by JavaScript.

var yourHTML = '<div class="mynewdiv">Test</div>';
document.getElementsByClassName('at-sticky' 'custom-logo-link') 
[0].innerHTML = yourHTML;
<div class="at-sticky"> 
  <div class="container"> 
    <div class="navbar-header"> 
      <a href="#" class="custom-logo-link" 
      rel="home" itemprop="url">
        <img src="#" class="custom-logo">
      </a>
    </div>
  </div>
</div>

1
  • What is ' ' ? Commented May 16, 2019 at 8:54

8 Answers 8

4

You can try with Document​.query​Selector() which allows CSS like selector:

var yourHTML = '<div class="mynewdiv">Test</div>';
document.querySelector('.at-sticky .custom-logo-link').innerHTML = yourHTML;
<div class="at-sticky"> 
  <div class="container"> 
    <div class="navbar-header"> 
      <a href="#" class="custom-logo-link" 
      rel="home" itemprop="url">
        <img src="#" class="custom-logo">
      </a>
    </div>
  </div>
</div>

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

2 Comments

In my case .at-sticky class is loading by Javascripting when scroll down. Maybe that is the reason it is not working on my end.
@IftekHar, it is really difficult to tell what the issue could be without observing that:)
1
Please use querySelector()

var yourHTML = '<div class="mynewdiv">Test</div>';
document.querySelector('.at-sticky .custom-logo-link').innerHTML = yourHTML;

Comments

0

Simplest solution is using querySelector

Example:

document.querySelector('.at-sticky .container .custom-logo-link').innerHTML = yourHTML

Comments

0

Please put the comma in between the class names:

document.getElementsByClassName('at-sticky', 'custom-logo-link')[0].innerHTML = yourHTML;

Comments

0

Ok 2 points: Firstly when using innerHTML you are inserting text into your element, not the full tag as you have done. Secondly you only need pass in to ‘document.getElementsByClassnames’ the class name that you are targeting. Dont worry about the parent div, that should work fine as is. I would have posted a comment but I don’t have enough reputation yet;)

Comments

0

You can use children() function for this.

$('.at-sticky').children();

Comments

0

You can use querySelector for this.

Let's say you have the following HTML structure

<div class="parentDiv">
  <a href="#"></a>
</div>

Then you can do this in JavaScript

let parentDiv = document.querySelector('.parentDiv')
let childLink = parentDiv.querySelector('a')

console.log(childLink)

Comments

0

Tip: Try printing the container's output using console.log, then scroll down to see the list of childNodes.

If you want to target the .custom-logo-link class, you must include the tag in the querySelector.

document.querySelector('.at-sticky a.custom-logo-link').innerHTML = yourHTML;

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.