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

Restore font-weight if a different list item is chosen

I previously inquired about setting the font-weight to bold on a text element when selected. Thanks to @Eric, this has been successfully implemented! However, I am facing an issue where clicking on one text makes it bold, and then clicking on another text ...

exercise on json result

Hello everyone, I have encountered the following output: ["images\/zara\/shoes\/thumbnail\/1.png","images\/zara\/shoes\/thumbnail\/2.png"] using this code snippet: $imgurl=array(); $i=0; if(mysql_num_rows($result) ...

Utilizing Jquery and .Net to Include a Server Variable in Ajax Request Data

How can I use a .Net server variable in the data of an Ajax call? $.ajax({ url: "get_user_info.aspx", data: "user_id=<%=UserID%>" }); When attempting the above code, I encounter an Object Expected error. The UserID is an integer in the C# cod ...

Adding dynamic elements to a pop-up window with the use of jQuery's .load() function

When implementing Javascript to create a modal, I have encountered an issue where the close button disappears. The modal opens and loads different HTML files dynamically using the jQuery load() method, but the close button vanishes. I suspect that the mod ...

Rearranging Twitter Bootstrap Grid Columns

Is it possible to rearrange columns in Bootstrap without using col-sm-pull-12/col-sm-push-12? I'm struggling to achieve the desired layout. Are there any alternative methods to accomplish this task? Check out this JSFiddle for reference. <div cla ...

What is the best way to ensure that the table header is printed on every page when printing?

My table has a large number of dynamic rows, and when I try to print or preview it using the window.print() function, only the table header (thead) appears on the first page. How can I make sure that the table header appears on each printed page? <div ...

Using CSS Clip Path to conceal elements

My current design uses clip-path for a gradient effect at the top of the page. Ideally, I want the two angles to meet seamlessly without any visual issues. I initially positioned the top so that they touch perfectly and nothing interferes with the angled ...

Using Selenium to scroll down to an element until its 'style' changes

I'm in the process of scraping a review page similar to this one. While this specific page has only a few reviews, there are others with a larger volume that require extensive scrolling. Upon observation, I noticed that when the page is not complete ...

Date Object Replacement Error: "this is not a valid Date object."

For my web application, I require a specific date value. I attempted to modify the Date prototype, but encountered an issue with Jquery Date recognizing it. InitDate = Date; InitDate.prototype = Date.prototype; Date = function () { if (arguments.leng ...

Developing dynamic checkbox components using jQuery - unusual behavior in Internet Explorer

I am dynamically creating checkbox elements using jQuery and appending them to a specified node as shown below var topics = ['All','Cat1','Cat2']; var topicContainer = $('ul#someElementId'); $.each( topics, functio ...

Build.js.erb requesting a partial script task

Struggling to make my create action function accurately in my rails app. The following two lines work as intended: $('#pit_form').remove(); //remove form $('#new_link').show(); //show new link again They successfully remove the form ...

Encapsulate every list item with a <ul> tag using jQuery

Not going into specifics, I've written some jQuery code like this: $("nav ul ul").each(function () { var navitems = $(this).html(); alert(navitems); }); I understand that .html uses InnerHTML so the alerts display as follows: <li>xxxx ...

Implementing file uploads using AJAX in CodeIgniter will continue to run, even if the value is null or undefined

I need to trigger another ajax request if the file has not been input yet. However, it is currently still carrying out the form action. Below is my view code: <form method="POST" id="quiz_file" action="<?php echo site_url('home/upload_quiz/&ap ...

jQuery Mobile - Implementing Persistent Toolbars along with a custom back button functionality

UPDATE Here is a fiddle of the problem: https://jsfiddle.net/9k449qs2/ - attempt to select the header using your picker, but every time you click it will select the whole page instead. I am currently working on a project that includes a persistent header ...

Executing a function on the window object in JavaScript

I have come across the following code and am seeking guidance on how to get the last line to function correctly. The API I am using currently employs _view appended as its namespacing convention, but I would prefer to switch to something like arc.view.$f ...

Explain the concept of grid layout in CSS

Can you please clarify the distinctions between grid and liquid layouts? Specifically, I am interested in understanding the concept of a grid layout. Thank you in advance for your explanation. ...

Implementing a JavaScript file and ensuring W3C compliance

I recently purchased a template that included a javascript file in the main page with the following code: <script src="thefile.js?v=v1.9.6&sv=v0.0.1"></script> Upon inspection, I noticed there are two arguments at the end of the file ...

Displaying a partial view only on the initial load is considered a best practice

Hey there, I have a view with some information and a container that can load two partial views - one at a time. I am able to load the first one (a form with parameters for a query) using $.ajax() in a function that I call within $(document).ready. $(docu ...

What causes the variation in results when I input CSS directly into the head section versus linking it from a separate .css file?

As a beginner, I am facing an issue with my CSS. When I directly insert the CSS into the head of my .html file, everything looks perfect. However, when I link my .css file into the head, the very first word ("Holy") does not display properly. I have double ...

Display div content depending on the clicked ID in AngularJS

Unique Header Links HTML <ul ng-controller="pageRouteCtrl"> <li ui-sref-active="active"> <a ui-sref="home" class="" ng-click="getPageId('live-view')">LIVE</a> </li> <li> <a ng-clic ...