You can use jQuery’s attr() function. For example, if your img
tag has an id
attribute of ‘my_image’, you would do this:
<img id="my_image" src="first.jpg"/>
Then you can change the src
of your image with jQuery like this:
$("#my_image").attr("src","second.jpg");
To attach this to a click
event, you could write:
$('#my_image').on({ 'click': function(){ $('#my_image').attr('src','second.jpg'); } });
To rotate the image, you could do this:
$('img').on({ 'click': function() { var src = ($(this).attr('src') === 'img1_on.jpg') ? 'img2_on.jpg' : 'img1_on.jpg'; $(this).attr('src', src); } });
Related Posts:
- Failed to load resource: net::ERR_CONTENT_LENGTH_MISMATCH
- Load image with jQuery and append it to the DOM
- How to change CSS using jQuery?
- How to properly use jsPDF library
- Cross-Origin Read Blocking (CORB)
- Uncaught ReferenceError: $ is not defined?
- What does [object Object] mean?
- Remove class using jQuery
- How do I check whether a checkbox is checked in jQuery?
- jQuery document.createElement equivalent?
- How to make history.back() without getting a warning on the browser to reload the page?
- convert Hsl to rgb and hex
- Pure JavaScript equivalent of jQuery’s $.ready() – how to call a function when the page/DOM is ready for it [duplicate]
- How can I scroll to an element using jQuery?
- jQuery Get Selected Option From Dropdown
- How can I refresh a page with jQuery?
- How to add jQuery code into HTML Page
- What does jQuery.fn mean?
- Setting “checked” for a checkbox with jQuery
- Jquery : Refresh/Reload the page on clicking a button
- Bootstrap Dropdown menu is not working
- jquery SlideToggle effect in upward direction?
- How can I determine if a variable is ‘undefined’ or ‘null’?
- Export html table data to Excel using JavaScript / JQuery is not working properly in chrome browser
- Set timeout for ajax (jQuery)
- jQuery Ajax File Upload
- How to use npm jquery module?
- Jquery swipe left or right on mobile
- How to move an element into another element?
- document.getElementByID is not a function
- Moment js date time comparison
- How to create a JQuery Clock / Timer
- bootstrap-datetimepicker for bootstrap 4
- Proper way to wait for one function to finish before continuing?
- Access Control Request Headers, is added to header in AJAX request with jQuery
- How to set ‘X-Frame-Options’ on iframe?
- How do I get an element to scroll into view, using jQuery?
- How do I check if string contains substring? [duplicate]
- explode string in jquery
- Play an audio file using jQuery when a button is clicked
- How can I get the last character in a string?
- document.getElementsByClassName().innerHTML always returns “undefined”
- Jquery button click() function is not working
- How to define Global Arrays?
- Why is this jQuery click function not working?
- Disable Chrome strict MIME type checking
- How to uncheck a radio button?
- Phone mask with jQuery and Masked Input Plugin
- JQuery get all elements by class name
- Attaching ‘onclick’ event to D3 chart background
- How do I check whether a checkbox is checked in jQuery?
- The preferred way of creating a new element with jQuery
- What is jQuery Unobtrusive Validation?
- jQuery count child elements
- How do I check if an element is hidden in jQuery?
- jQuery equivalent of querySelector
- Convert normal date to unix timestamp
- jQuery replace one class with another
- Clearing my form inputs after submission
- Get class list for element with jQuery
- Convert array to JSON
- How to disable scrolling temporarily?
- How do you remove all the options of a select box and then add one option and select it with jQuery?
- Add and remove multiple classes in jQuery
- How to get unique values in an array
- jQuery: Get selected element tag name
- Why is jquery show() not working in example
- How can I use jQuery to run MySQL queries?
- jQuery if Element has an ID?
- Complex JSON nesting of objects and arrays
- jQuery – Increase the value of a counter when a button is clicked
- Print a div content using Jquery
- Get the size of the screen, current web page and browser window
- How can JavaScript save to a local file?
- How to reload/refresh jQuery dataTable?
- How to find if div with specific id exists in jQuery?
- TypeError: $ is not a function WordPress
- Fully responsive HTML5 video
- HTML anchor link – href and onclick both?
- How can I know which radio button is selected via jQuery?
- jquery toggleClass not working
- JQuery Number Formatting
- How to add jQuery in JS file
- Get query string parameters url values with jQuery / Javascript (querystring)
- How to use aria-expanded=”true” to change a css property
- How can I get my Calculate Contact Form 7 field to update with jQuery? [closed]
- Contact Form 7 – Uncaught TypeError: wpcf7.initForm is not a function [closed]
- AJAX not Working with php as supposed
- is_page_template() written in jquery/javascript
- Cannot use characters ” in jQuery script in WordPress
- On click load iframe [closed]
- How to set javascript options for select2 in PHP array of plugin
- Script only works on wordpress home page & only when Jqurery is aded using
- how to prevent added meta box from being injected with style=”position: fixed; top: 56px;” at scroll down
- internal anchor links no longer working after upgrade
- Error on clicking ‘Screen Options’ tab only at Products-related editor screen(s) and for specific user
- Remove escape characters from JSON string pulled from a database
- Select parent from child class name then add another class name to the parent
- Access jQuery tabs from primary menu while adding/removing class on tab container elements (accessing diff. tabs/tab content via primary menu items)