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
- What’s the difference between “{}” and “[]” while declaring a JavaScript array?
- How do I check if an array includes a value in JavaScript?
- Do I just put the html in a page when enqueueng or do I also have to reference js file from the html page [closed]
- How to Add a Read-only Textbox With Text in it That Has a Button to Copy it?
- Make a script work within a page
- WP adds long version query strings to CSS and JS files
- Link to page with posts of specific type
- prevent HTML output if custom field is empty [closed]
- creating a static HTML/JS/PHP page
- WordPress getting css and script files from client machine rather than the server machine
- how to let users upload their custom cover image
- Putting custom html/js page into Elementor as it’s own block
- Add Paypal button redirect to a specific page after completing
- Hide Tab Titles from Tabs Block
- Add other class content with reference class value
- Web cover page open when button clicked
- internal anchor links no longer working after upgrade
- How to run Javascript popup modal in a loop?
- Overriding inline !important CSS with Javscript code snippet with media query
- Landing Page with HTML CSS JS and Images on wordpress
- How to make wocommerce shop products same heigt
- Can’t Listen to KeyDown in TInyMCE Iframe (jQuery) and Pass it to Parent HTML FORM
- Unable to use Datepicker in a table when inserting a row
- WordPress Slider is not working
- Getting click on item to open closest relevant element (popup) with jQuery
- jQuery for Fade Preload Causes My Site Favicon to Disappear from Tab of Browser
- Target a specific container to style on WordPress storefront theme
- Site images are fixed in front of my site content [closed]
- How would I add this code to a single WordPress page?
- How to delay display of page elements until enqueued script has injected html
- Trouble with adding preloader to my home page index.html and index.js
- Where to find the html for WordPress site? [closed]
- how to do open a link in a open window from menu bar? [closed]
- Import HTML code that loads css and javascript into specific page
- Possible jquery / library conflict in plugin usage
- Trying to customize wordpress drop down categories
- Element works, but not on wordpress site [duplicate]
- How to keep close a sub-menu under homepage? [closed]
- Making custom pages (with an example to explain the goal)
- Interactive maps in wordpress
- 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]
- Can I upload my HTML/CSS/Javascript game onto a WordPress page?
- 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?
- Modal pop-up HTML code works on other sites or HTML viewers but not in the custom HTML block within a wordpress page?
- Sending GravityForms to custom HTML
- Processing javascript on wordpress
- Identify and style the paragraph beginning w/o a class: based on a combination of common characters (CSS, JS)
- How to use Javascript to get data from a WordPress form