I'm having trouble getting my jQuery click handler to work

Here is the HTML content within my Sharepoint 2010 Web Part project:

<html>
  <h1>Travel Form Assistance</h1>
  <div id="preTravel" name="preTravel">
    <h2>Pre Travel</h2>
    <img id="imgPreTravel" name="imgPreTravel" src="/_layouts/images/TravelFormHelp/posttravelpdf.png" alt="pre Travel image" height="275" width="350">
  </div>
  <div id="postTravel" name="postTravel">
    <h2>Post Travel</h2>
    <img id="imgPostTravel" name="imgPostTravel" src="/_layouts/images/TravelFormHelp/posttravelpdf.png" alt="post Travel image" height="275" width="350">
  </div>
</html>

...along with this jQuery script:

<script type="text/javascript">
    $('imgPostTravel').click(function () {
        alert('you clicked the post Travel image');
        this.addClass('hide');
    });
</script>

The relevant CSS code is as follows:

.hide {
  visibility: hidden;
  display: none;
}

Despite implementing the onclick event for 'imgPostTravel', nothing seems to happen upon clicking - the image remains visible and the alert does not pop up.

To double-check my logic, I ran a test on jsfiddle here, only to encounter the same issue where the click handler fails to execute. There appears to be an underlying mistake in my approach, but pinpointing it has proven challenging...

Answer №1

Just a quick solution - make sure to include a hashtag in your selector as it is an ID:

$('#imgPostTravel').click(function () {

Answer №2

Try updating your code in this way

<script type="text/javascript>
        $(document).ready(function() {
            $('#imgPostTravel').on('click', function() {
                alert('You clicked the post Travel image');
                $(this).addClass('hide');
            });
        });
</script>

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

Footer not adhering to the bottom of specific pages

I have been using the code snippet below to ensure that most of my pages stick to the bottom. However, I've noticed that the ones that don't are sub-menu items that contain a contact form with captcha. I'm not sure what's causing this i ...

Calculate the total cost for each row in a table by multiplying the quantity and price using jQuery, then display the result

I'm encountering an issue with my table row calculations involving price and quantity. The calculation results show up correctly in the console, but when I try to display them back on the table, the total sum of the first row gets duplicated into all ...

Use jQuery to swap out every nth class name in the code

I am looking to update the 6th occurrence of a specific div class. This is my current code <div class="disp">...</div> <div class="disp">...</div> <div class="disp">...</div> <div class="disp">...</div> < ...

Styling CSS to place an image in between text and background coloring

As I try to replicate a happy accident of mine, which originally occurred during my first attempt at CSS: I was just randomly adding selectors when I stumbled upon this unique layout. However, as I proceeded with rewriting the file, I failed to save the o ...

Accessing ASPX functionality through jQuery AJAXLet's see how we

I've been attempting to use jQuery and AJAX to call a method in a separate ASPX file, but despite following numerous tutorials with the same steps, I just can't seem to get it working. Here's how I set it up: <input class="myButton"> ...

The continuation of unraveling the mystery of utilizing `overflow-y:scroll` in a horizontal slider

As a beginner, I realized too late that adding code in comments is not an option. Consequently, I decided to create a new question and ask for your assistance once again. To optimize the use of space, I have implemented bxSlider with fixed dimensions (wid ...

The input form is experiencing issues with the .change event on the drop down selector, specifically with dynamically generated choices

Currently, I am working on a form that allows users to add or remove up to three vehicles. Each vehicle requires the user to select a year, make, and model. However, I have encountered an issue where if a user selects three vehicles and then decides to cha ...

Displaying a JQuery notification when hovering over a link

I am having trouble getting an alert to pop up when I hover over a hyperlink using JQuery and Javascript. The hyperlink is inside an anchor within the main section of the HTML. Any assistance would be much appreciated. Here is my current code snippet: &l ...

Printing Apex Oracle reports using Internet Explorer

I am facing a challenge with printing my page that contains two interactive reports. To enable printing, I have utilized a JavaScript function that triggers window.print(). I have incorporated CSS to adjust the layout of my tables when printed. @media pr ...

Problem with icon color not being applied to lightning-tab component in LWC

.html <lightning-tabset variant="vertical" > <lightning-tab class="class1" icon-name="utility:adduser" label="demo label1"> demo label1 </lightning-tab> <lightning-tab class=" ...

Tips for utilizing <%= item %> in Jquery within EJS framework

Is there a way to utilize <%= item %> in jQuery within EJS? I understand how to use it within <html> tags, but I am curious if it can be utilized within <script> tags as well. <strong> <%= userx.email %></strong> Wha ...

Enhance your Redux Framework sliders with wp_editor instead of a basic textfield for a more

I am currently working on customizing the default field for slides in the Redux framework. My goal is to integrate the WordPress editor instead of a simple textarea for the description area. The original file can be found here: https://raw.githubuserconte ...

Leveraging createMuiTheme to customize default styles for divs, paragraphs, and the body element

Seeking guidance on customizing a Material UI Theme I aim to modify the default styles for elements like <body>. Currently, at the root of my React tree: import theme from './mui-theme' ReactDOM.render( <Router> <ThemePr ...

Display the text after passing the jQuery validation test

Currently, I have successfully implemented the jquery.validate.js plugin and it is functioning properly. However, my goal is to display text in a hidden div only when validation is successful and the form has been submitted. $("#commentForm").validate(); ...

Having issues with contenteditable functionality not functioning properly on elements that are dynamically generated

Creating an unordered list dynamically and adding items to it on a button click. Appending it to a section with contenteditable set to true, but encountering issues. The code snippet below demonstrates the process: // Create text input var categoryInput = ...

Superceding Meta Character Encoding Statements

Have you ever wondered what happens when two older-style character encoding declarations are used in a webpage? Will the first declaration override the second, or vice versa? Or will they both be ignored and excluded from the encoding algorithm? For Ex ...

Exploring the capabilities of qTip2 integrated with jQuery Vector Maps (jqvmap)

Is there a way to disable the default tooltip in jqvmap and replace it with qTip2? Check out this example. Here's the jQuery code: jQuery('#vmap').vectorMap({ map: 'world_en', backgroundColor: null, color: '#ffff ...

Display the HTML/CSS layout following the JavaScript window.open action

Encountering issues with printing output in an opened window using JavaScript. I'm creating a new document through window.open and including CDN links to Bootstrap files. While everything appears fine in the opened window, when attempting to print (XP ...

Guide on reinitiating the Ajax call when encountering a Time out error

In my HTML file, I have a document ready function that looks like this: $(document).ready(function() { cust_id = getParameterByName('customer_id'); var locationAjaxCall = $.ajax({ type: 'GET', url: url+'/O ...

What is the best way to align the navbar items at the center in Bootstrap when mx-auto is not working?

<nav class="navbar navbar-expand-sm navbar-light"> <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls ...