Add your <script>
to the bottom of your <body>
, or add an event listener for DOMContentLoaded
following this StackOverflow question.
If that script executes in the <head>
section of the code, document.getElementsByClassName(...)
will return an empty array because the DOM is not loaded yet.
You’re getting the Type Error
because you’re referencing search_span[0]
, but search_span[0]
is undefined
.
This works when you execute it in Dev Tools because the DOM is already loaded.
Related Posts:
- How can I scroll to an element using jQuery?
- Uncaught TypeError: Cannot read property ‘top’ of undefined
- Retrieve the position (X,Y) of an HTML element
- 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)
- jQuery document.createElement equivalent?
- Include another HTML file in a HTML file
- 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]
- Bootstrap Dropdown menu is not working
- Change Background color (css property) using Jquery
- 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
- Completely disable scrolling of webpage
- How can I select an element by name with jQuery?
- Full-screen iframe with a height of 100%
- How to print HTML content on click of a button, but not the page?
- Check if an element contains a class in JavaScript?
- Bootstrap 4 Dropdown Menu not working?
- Parse an HTML string with JS
- Why is the jquery script not working?
- Textarea Auto height
- Why is marquee deprecated and what is the best alternative?
- Refresh/reload the content in Div using jquery/ajax
- How to write inside a DIV box with javascript
- How to get height of div in px dimension
- Crop the image using JavaScript
- Force div to be topmost element of any and all webpages
- How to display raw JSON data on a HTML page
- how to display a div triggered by onclick event
- Why is jquery show() not working in example
- jQuery scrollTop() method not working
- Change an image with onclick()
- JavaScript – Count Number of Visitor for Website
- Creating Accordion Table with Bootstrap
- Getting or changing CSS class property with Javascript using DOM style
- Fully responsive HTML5 video
- How to create checkbox inside dropdown?
- Positioning element at center of screen
- Adding Google Translate to a web site
- How to make HTML element resizable using pure Javascript?
- How to use aria-expanded=”true” to change a css property
- How to set “style=display:none;” using jQuery’s attr method?
- WordPress Bootstrap Handburger Menu Wont Open
- Hide CSS class only in single post pages
- Angular: conditional class with *ngClass
- How to fix “Cannot read property ‘addEventListener’ of null” error [duplicate]
- Remove class using jQuery
- How do I check whether a checkbox is checked in jQuery?
- How to play audio?
- How to play audio?
- How do I test for an empty JavaScript object?
- How to add jQuery code into HTML Page
- “Uncaught TypeError: Cannot read property ‘style’ of null at.. “
- jquery SlideToggle effect in upward direction?
- HTML5 Local storage vs. Session storage
- Express Render not working return error: No default engine was specified and no extension was provided
- Jquery swipe left or right on mobile
- How to move an element into another element?
- How can I display a JavaScript object?
- How to make JavaScript execute after page load?
- How to move all HTML element children to another parent using JavaScript?
- document.getElementsByClassName().innerHTML always returns “undefined”
- Error: ENOENT: no such file or directory, stat ‘/public/main.html’ at Error (native)
- onClick to get the ID of the clicked button
- HTML text input allow only numeric input
- How can I change an element’s class with JavaScript?
- How to pass parameters using ui-sref in ui-router to controller
- What does ngf-select do and why is it needed for form validation?
- jQuery: Uncaught Error: Syntax error, unrecognized expression
- Creating a div element in jQuery
- How to use in jQuery :not and hasClass() to get a specific element without a class
- Adjust width and height of iframe to fit with content in it
- document.getElementbyId() returning null
- Get class list for element with jQuery
- HTML/Javascript change div content
- How To Save Canvas As An Image With canvas.toDataURL()?
- How to embed a youtube playlist with a sidebar
- External JavaScript Not Running, does not write to document
- How can I use jQuery to run MySQL queries?
- Create a simple 10 second countdown
- Print a div content using Jquery
- What’s the effect of adding ‘return false’ to a click event listener?
- HTML anchor link – href and onclick both?
- How can I know which radio button is selected via jQuery?
- How can I remove a style added with .css() function?
- How to wrap the content of the main tinyMCE editor with extra tags
- How do developers add “Smooth Transitions” to their sites?
- Building a slide down search box in wordpress
- Web cover page open when button clicked
- How to run Javascript popup modal in a loop?
- How to make wocommerce shop products same heigt
- Where to find the html for WordPress site? [closed]
- Import HTML code that loads css and javascript into specific page
- How to keep close a sub-menu under homepage? [closed]
- Interactive maps in wordpress
- I have custom html mixed with inline javascript that I want added to my WordPress site, how do I achieve this?