Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Salesforce
  3. Question
Salesforce

Getting “TypeError: Cannot set property 'onclick' of null”

Asked by Hara Uchida Aug 17, 2021 1.4K views 1 answer
Share

About this question

I am getting a visualforce error, cannot set property 'onclick' of null

I have defined an onclick event on a DOM element. Here is my [removed]

(function (window, document) { var layout = document.getElementById('layout'); var menu = document.getElementById('menu'); var menuLink = document.getElementById('menuLink'); function toggleClass(element, className) { var classes = element.className.split(/s+/), length = classes.length, i = 0; for(; i < length xss=removed xss=removed xss=removed active = 'active' xss=removed>

The line causing the error is: menuLink.onclick = clickOnMenu; The error shown in chrome is TypeError: Cannot set property 'onclick' of null.

I don't understand why the menuLink variable is null. In Chrome console, when I type console.log(menuLink), the variable is not null and contains the DOM object in question.

I included my javascript file at the end of the visualforce page :

Note that the exact same code works fine on a local environment! (html instead of visualforce). 

Your answer

1 Answer

More Salesforce discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Salesforce Blogs

Guides, tips and career advice on Salesforce from JanBask experts.