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

How to solve Getting “TypeError: Cannot set property 'onclick' of null” error?

Asked by Aashish Chaursiya Aug 16, 2021 858 views 1 answer
Share

About this question

 I have a Visualforce error that's driving me crazy. My Javascript file ui.js (see below) is just defining 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 the 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).

Thanks in advance for any help. 

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.