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
- Find object by id in an array of JavaScript objects
- Cannot set property InnerHTML of null [duplicate]
- Pure JavaScript equivalent of jQuery’s $.ready() – how to call a function when the page/DOM is ready for it [duplicate]
- Remove CSS class from element with JavaScript (no jQuery) [duplicate]
- How to make JavaScript execute after page load?
- Javascript: getElementById vs getElementsById (both works on different pages)
- Export html table data to Excel using JavaScript / JQuery is not working properly in chrome browser
- querySelector vs. getElementById
- How to generate a simple popup using jQuery
- TypeError: Cannot Set property ‘onclick’ of null
- Bootstrap Dropdown menu is not working
- Bootstrap Dropdown menu is not working
- Create table with jQuery – append
- How would I call a javascript function in html?
- Encode HTML entities in JavaScript
- “.addEventListener is not a function” why does this error occur?
- How can I check if an element exists in the visible DOM?
- Coin toss with JavaScript and HTML
- Play an audio file using jQuery when a button is clicked
- getBoundingClientRect is not a function
- Jquery button click() function is not working
- Make iframe automatically adjust height according to the contents without using scrollbar?
- Change text on hover, then return to the previous text
- Mixed Content Page: requested an insecure stylesheet error
- How do I check whether a checkbox is checked in jQuery?
- How to create a HTML Cancel button that redirects to a URL
- jQuery count child elements
- Scroll to bottom of div?
- How to run a function when the page is loaded?
- Clearing my form inputs after submission
- Reload an iframe with jQuery
- Video auto play is not working in Safari and Chrome desktop browser
- How to change the buttons text using javascript
- “Uncaught TypeError: a.indexOf is not a function” error when opening new foundation project
- How to change the text of a label?
- Using “×” word in html changes to ×
- How do I store an array in localStorage?
- Changing the selected option of an HTML Select element
- img onclick call to JavaScript function
- Bootstrap Collapse not Collapsing
- How to scroll to top of page with JavaScript/jQuery?
- How to embed an autoplaying YouTube video in an iframe?
- How do I clear the content of a div using JavaScript?
- The value of attribute [ data-type ] must be in double quotes – custom html widget error
- Adding JavaScript to a WordPress website
- Insert “javascript:void(0);” into URL
- Can I change an external script’s HTML?
- How to integrate my HTML and javascript into my child theme
- how to do open a link in a open window from menu bar? [closed]
- Trying to customize wordpress drop down categories
- Making custom pages (with an example to explain the goal)
- Modal pop-up HTML code works on other sites or HTML viewers but not in the custom HTML block within a wordpress page?