jQuery functions correctly within jsfiddle, yet it encounters issues when utilized within WordPress

I've been experimenting with a jQuery script to grey out the screen. While it works perfectly on my jsFiddle link provided below, I'm having trouble getting it to work from within my WordPress plugin.

Check out the working script on my jsFiddle here

This is the PHP code I'm using to include the JavaScript:

function myScripts() {
    wp_enqueue_script('myScript', plugins_url('myPlugin/JS/myScript.js'), array('jquery'), true);
}

add_action('wp_enqueue_scripts', 'myScripts');

Here's the HTML code snippet:

<div id="box"></div>
<img id="something" src="http://www.w3schools.com/images/w3schools.png" />

And this is the content of myScript.js file, which contains the jQuery code:

$(document).ready(function() {
    $('#something').click(function() {
        $('#box').show();
        $('#box').fadeTo(500, 0.5);
    });
});

Answer №1

Consider implementing the following JavaScript snippet:

document.addEventListener("DOMContentLoaded", function(){
    document.getElementById('something').addEventListener('click', function(){
        document.getElementById('box').style.display = 'block';
        document.getElementById('box').style.opacity = 0.5;
    });
});

This approach ensures compatibility and prevents conflicts by utilizing vanilla JavaScript instead of relying on jQuery's shorthand $ selector.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Utilizing an offline HTML file instead of a URL within Objective-C programming

Is there a way to modify this code to utilize an offline (bundled) file instead of the "http://5times9.com"? - (void)loadAddWeb { UIWebView *addWeb = [[UIWebView alloc] init]; NSURL *webURL = [NSURL URLWithString:@"http://5times9.com"]; NSURLRequest *we ...

(Javascript - Arrays) Find the leftmost and rightmost connected characters

Looking for the leftmost and topmost connected '1' character in a 2D matrix? Find the most left & top connected '1' character Find the most right & bottom connected '1' character EDIT 2.0: To start, provide the coordina ...

Issues with Joomla's mobile menu compatibility

We are currently working on a Joomla website that can be accessed through this link. The site functions perfectly with two menus when viewed on a desktop screen. However, the issue arises when it is accessed from a smartphone like an iPhone 5, as instead o ...

When adjusting the top margin of the main content in CSS, the fixed top navigation section's margin also decreases simultaneously with the main content

I am in the process of creating a basic static blog page using HTML5, SASS, and CSS. While working on the design, I utilized the 'nav' semantic element for a fixed top bar on the page that remains visible even when scrolling down. Additionally, ...

What sets the target property of the mousewheel event apart from other events like click, mousedown, and touchstart?

The mousewheel event's target property reveals the DOM element currently being hovered over when using the mousewheel or gesture-capable touchpad. In my experience (specifically in Safari 6, I will verify this in other browsers later), the target ret ...

Dot/Bracket Notation: flexible key assignments

I have an item that contains various image URLs accessed through data[index].gallery.first.mobile/tablet/desktop "gallery": { "first": { "mobile": "./assets/product-xx99-mark-two-headphones/mobile/image-gallery-1.j ...

What's the best way to display a bootstrap modal window popup without redirecting to a new page?

I am having trouble implementing a modal window that will display validation errors to the user when they submit a form. Currently, the window is opening as a new view instead of overlapping the existing form's view. How can I adjust my code so that t ...

Steps for incorporating jQuery files into Angular 4

As a beginner in Angular 4, I am faced with the challenge of calling a jQuery function using an HTML tag from a method. The jQuery method is located in a separate file. How can I incorporate this into my Angular project? Here's an example: sample() { ...

Meteor chat platform now offers the option to create multiple chat rooms

I've been working on an application that features multiple chat rooms. Currently, one of the rooms is functional in terms of sending and receiving messages. However, when I navigate to a different room and try to send a message, the message doesn&apos ...

Steps for incorporating events on Jquery UI button

I have a question about utilizing Jquery UI for the first time. After successfully adding a button using Jquery UI, I am now interested in adding events for when a radio switch is turned on and off. When the radiobox is switched on, I want an alert window ...

The $geoNear operator must be the initial stage in a pipeline to be valid

When performing an aggregation using nodejs as the server-side language, I encountered the error message $geoNear is only valid as the first stage in a pipeline. This is the Aggregation Object: [ { '$geoNear': { near: [Object], distanceFie ...

Steps for developing a floating image transition using CSS and React

Looking to achieve a floating image transition using CSS and React, similar to the Divi theme header images. Check out the example below: enter image description here For more information, visit: ...

Converting JSON data into objects in JavaScript

My task involves processing a large JSON list with over 2500 entries, formatted as follows: [ ['fb.com', 'http://facebook.com/'], ['ggle.com', 'http://google.com/'] ] This JSON list represents pairs of [&ap ...

When the page is loaded, populate FullCalendar with events from the model

On page load, I am attempting to populate events with different colors (red, yellow, green) on each day of the calendar. Here is a simple example showcasing events for three days: https://i.sstatic.net/YzJ4E.png I have data in a model that indicates the ...

Guide on creating a toggle effect for a div with querySelector and addEventListener

I utilized the email and password divs provided by Bootstrap. The CSS for the id dropdownlogin includes display: none; in this post, I hope that I have shared adequate information. <script> document.addEventListener('DOMContentLoaded', ...

Update a roster with AJAX following the addition or removal of a user

My goal is to implement two functions: Adding and Deleting a User from the database using Mongoose. However, when I run the code, I receive a 200 OK response but with an empty username. I suspect there might be an issue with the ajax calls. Can anyone hel ...

Changing the CSS background in React when updates occur

Currently working on toggling the CSS background image within this React application. The images have been successfully imported and a variable called "image" is set to the imported image as the iteration increases/decreases with each click. The issue I&a ...

Why does my express POST request result in an empty req.body in Node.js?

After confirming that my data is being passed correctly and the db connection is successful, I am facing an issue with my ajax request. Even though the success callback returns the id, my data seems to not be passing through properly. When attempting to a ...

React and Express: Encounter a proxy error when trying to proxy the request for /total from localhost_errors

This question is a continuation of my previous inquiry on React.js: Axois Post stay on pending(but i am getting data) Below is the content of my package.json file { "name": "my-app", "version": "0.1.0", "private": true, "dependencies": { "ax ...

Develop a program in Python using Selenium to automate web scraping in a loop

I am looking to create a loop that will allow me to extract the individual time figures for each horse in all eight races from the at the races website. Here is an example of the first race (17:15) out of the eight: from selenium import webdriver from s ...