The problem with your code is that the your script is executed prior to the html element being available. Because of the that var comment
is an empty array.
So you should move your script after the html element is available.
Also, getElementsByClassName
returns html collection, so if you need to add event Listener to an element, you will need to do something like following
comment[0].addEventListener('click' , showComment , false ) ;
If you want to add event listener to all the elements, then you will need to loop through them
for (var i = 0 ; i < comment.length; i++) { comment[i].addEventListener('click' , showComment , false ) ; }
Related Posts:
- Failed to load resource: the server responded with a status of 404 (Not Found)
- Failed to load resource: the server responded with a status of 404 (Not Found)
- Maximum call stack size exceeded error
- How to properly use jsPDF library
- Can I use complex HTML with Twitter Bootstrap’s Tooltip?
- Google Chrome Uncaught (in promise) DOMException while playing AUDIO
- How to make a word underline in Markdown
- ChartJS beginAtZero, min, max doesn’t work
- How do I check whether a checkbox is checked in jQuery?
- how to fix : ” TypeError: Cannot read property ‘addEventListener’ of null”…?//
- Cannot set property InnerHTML of null [duplicate]
- ReferenceError: $ is not defined
- How to fix “Cannot read property ‘addEventListener’ of null” error [duplicate]
- React.js: Set innerHTML vs dangerouslySetInnerHTML
- How do I check whether a checkbox is checked in jQuery?
- jQuery document.createElement equivalent?
- (Google Map API) Geocode was not successful for the following reason: REQUEST_DENIED
- How to make a word underline in Markdown
- Pure JavaScript equivalent of jQuery’s $.ready() – how to call a function when the page/DOM is ready for it [duplicate]
- How to play audio?
- How to play audio?
- How can I scroll to an element using jQuery?
- jQuery.click() vs onClick
- jQuery Get Selected Option From Dropdown
- Include another HTML file in a HTML file
- Check if checkbox is checked with jQuery
- Add swipe left/right to web page, but use default swipe up/down
- How to add jQuery code into HTML Page
- How can I get the data-id attribute?
- Check if checkbox is checked with jQuery
- How to resolve the C:\fakepath?
- What is the meaning of == $0 that is shown in inspect element of google chrome for the selected element [duplicate]
- Angular: mat-form-field must contain a MatFormFieldControl
- (change) vs (ngModelChange) in angular
- Bootstrap Dropdown menu is not working
- jquery SlideToggle effect in upward direction?
- Change Background color (css property) using Jquery
- Failed to load resource: net::ERR_FILE_NOT_FOUND loading json.js
- Get class name using jQuery
- How to clear the canvas for redrawing
- Adding options to select with javascript
- Export html table data to Excel using JavaScript / JQuery is not working properly in chrome browser
- Set timeout for ajax (jQuery)
- How does the data-toggle attribute work? (What’s its API?)
- CSS height 100% percent not working
- JavaScript get element by name
- Use images like checkboxes
- How to read xml file contents in jQuery and display in html elements?
- HTML5 Local storage vs. Session storage
- Completely disable scrolling of webpage
- TypeError: Cannot Set property ‘onclick’ of null
- Express Render not working return error: No default engine was specified and no extension was provided
- How do I scroll to an element using JavaScript?
- Jquery swipe left or right on mobile
- Programmatically change the src of an img tag
- display variable image using cookies
- How to move an element into another element?
- character counter – backspace doesn’t reflect on characters remaining
- Open URL in same window and in same tab
- Create table with jQuery – append
- Error: Failed to execute ‘appendChild’ on ‘Node’: parameter 1 is not of type ‘Node’
- How to convert selected HTML to Json?
- How would I call a javascript function in html?
- How to Open New window on every Click
- Encode HTML entities in JavaScript
- Uncaught TypeError: Cannot read property ‘top’ of undefined
- How to move all HTML element children to another parent using JavaScript?
- Cannot read property ‘getContext’ of null, using canvas
- How can I select an element by name with jQuery?
- Full-screen iframe with a height of 100%
- Coin toss with JavaScript and HTML
- How to print HTML content on click of a button, but not the page?
- jQuery disable/enable submit button
- Play an audio file using jQuery when a button is clicked
- How to move an element after another element using JS or jquery?
- Bootstrap 4 Dropdown Menu not working?
- How to use Javascript to read local text file and read line by line?
- Parse an HTML string with JS
- Why is the jquery script not working?
- Shortest way to print current year in a website
- getBoundingClientRect is not a function
- DOMException: Failed to load because no supported source was found
- document.getElementsByClassName().innerHTML always returns “undefined”
- Jquery button click() function is not working
- Make iframe automatically adjust height according to the contents without using scrollbar?
- Vue is not defined
- How do I modify the URL without reloading the page?
- Textarea Auto height
- OnChange event handler for radio button (INPUT type=”radio”) doesn’t work as one value
- How to delay display of page elements until enqueued script has injected html
- how to do open a link in a open window from menu bar? [closed]
- Making the HTML list to a checkbox tree with the plugin jstree [closed]
- Why the images on mi javascript are not found?
- dropdown does not work [closed]
- How do I cycle a JS function in WordPress? [closed]
- Styling parent based on a child element of a child in a WordPress page
- I have custom html mixed with inline javascript that I want added to my WordPress site, how do I achieve this?
- Sending GravityForms to custom HTML
- Processing javascript on wordpress
- How to use Javascript to get data from a WordPress form