Solved

Adding custom Javascript to WordPress site to control image mouseover

Posted on 2014-04-15
5
695 Views
Last Modified: 2014-04-17
Here is a test page:
http://www.berksfoods.com/test/special-offers

When the page loads, the very first slider image is this one:
http://www.berksfoods.com/test/wp-content/uploads/2014/03/ph_promo001b1.jpg

I did not build this website. But I would like to attach my own Javascript mouseover event to this image. When I right-click on the slider itself, I don't even see "open image in new window", so not sure if the image is accessible to a mouseover event?

Is what I am describing possible? And if so, where in the existing WordPress theme/admin area would I add my Javascript? I know how to do this in a regular HTML/CSS/JS website, but trying to figure out how to do it in WordPress seems to be a whole different thing.

Thank you!
0
Comment
Question by:bbdesign
  • 2
  • 2
5 Comments
 
LVL 17

Accepted Solution

by:
jrm213jrm213 earned 500 total points
ID: 40003944
Hi,

So that page has a slew of javascript on it already and the way the slider works it doesn't give a very good way to select that image, but this is what I came up with, it appears to work if I run it through the console when viewing the page.

Basically I am adding the hover to the div with a class of ls-slide that contains an image with a src of ph_promo001b1.jpg

jQuery("div.ls-slide:has(img[src*='ph_promo001b1.jpg'])").hover(function(){alert('hello');},function(){alert('goodbye');});

Open in new window


The first function with alert('hello') is the onmouseover, the second function is the onmouse out. You have to supply both functions as parameters to the "hover" function, if you don't want anything to run onmouseout, just leave that function empty, ie

jQuery("div.ls-slide:has(img[src*='ph_promo001b1.jpg'])").hover(function(){alert('hello');},function(){});

Open in new window

0
 
LVL 51

Expert Comment

by:Julian Hansen
ID: 40003973
The image is inside an <a> element which is why you can't see it in the context menu.

What would like to do with the hover event? Rather than explain how to attach a mouseover event in this case - rather tell us what you want to do when you mouseover the image.
0
 

Author Comment

by:bbdesign
ID: 40004071
I want to swap out the image on mouseover (hover effect), to a different image.

I also need to know where to put the Javascript. I am a WordPress beginner, so need to know where in the admin interface it is best to do this? I guess I need to do it in such a way that if the Theme is upgraded, etc., the Javascript doesn't go away.

Thanks!
0
 

Author Comment

by:bbdesign
ID: 40007216
Thanks! With a little finagling, I was able to get this to work.
0
 
LVL 17

Expert Comment

by:jrm213jrm213
ID: 40007744
Glad to hear it! Thanks for the points.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In order to have all security and back ups taken care of, WordPress users can sign up for services with WP Engine.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The purpose of this video is to demonstrate how to add AdSense Ads to a WordPress Website, and how to set up WordPress to automatically place Ads in Sidebars. This will be demonstrated using a Windows 8 PC. Log into your AdSense account. : Cli…
The purpose of this video is to demonstrate how to integrate Mailchimp with WordPress, by placing a Mailchimp signup form on a WordPress Page or Post. This will be demonstrated using a Windows 8 PC. Mailchimp will be used. Log into your Mailchi…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now