Resize image to fit the window, and subsequently switch between full size and adjusted size upon clicking

I need assistance in creating a code snippet that will automatically adjust the size of an image to fit the user's screen resolution. Additionally, I want to provide users with the option to toggle between viewing the actual image size and the adjusted image size for their resolution. Here is what I have been working on:

<img src="<?=$file['direct_url']?>" border="0" onload="if(this.width>screen.width/1.5) {this.width=screen.width/1.5;this.alt='Click image to view full size';}window.status=this.width;" onmouseover="if(this.alt) this.style.cursor='hand';" onclick="if (this.width=screen.width/1.5) {this.width=screen.width*1.5} else {this.width=screen.width/1.5}">

Any assistance or suggestions would be highly appreciated!

Answer №1

One practical solution could be to incorporate a unique attribute that holds the original image dimensions.

<img sourcewidth="100" sourceheight="100" />

To access and utilize these values, you can employ the following code snippets:

this.getAttribute('sourcewidth');
this.getAttribute('sourceheight');

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

A dropdown menu generated from a Google Sheet script that allows for the input of custom text alongside predefined

Currently, I am working on creating a dropdown list that not only allows me to select items but also input free text. The code below enables me to choose an item from a selected range in Google Sheets. However, I am looking for a way to incorporate the f ...

Is it possible to incorporate swigjs within scripts?

Currently, I am stuck while working on my website using a combination of nodejs, express, and swigjs. The issue I am facing involves a <select> element that is populated by options from a variable passed to my template. When a user selects an option, ...

When attempting to browse for image files, Postman fails to display images

While trying to upload image files through Postman, I encountered an issue where the browser did not display any image files. It's important to note that I am using Ubuntu as my operating system. https://i.sstatic.net/1D3ro.png When I clicked on "se ...

Encountered a Node JS error when executing the command { PORT=4001 npm run test}

const app = express(); const port = process.env.PORT || 4000; app.listen(port, function(){ console.log('Listening on port ' + port); }); When I execute this command: $ PORT=4001 npm run test I encounter the following error: PORT=4001 : T ...

Asynchronous functions within the next context

Hello there! I am trying to send the client's IP address from the frontend in a Next.js application to the backend. To retrieve the IP, I am using the following function: async function getIP() { var clientIP = await publicIp.v4(); ...

"Oops! Looks like there's a reference error - the function you're trying

Javascript: function insertSmiley(a) { var $img = $(a).children('img'); $("#message").insertAtCursor(($("#message").data("wbb").options.bbmode) ? $("#message").data("wbb").toBB($(a)): $(a).html()); return false; } HTML: <a href= ...

Unable to maintain active status on Vuejs (PrimeVue) Sidebar component leads to malfunction

I currently have a PrimeVue Sidebar component set up with a dynamic component being passed to it. At the moment, I am only using a single component to test this functionality. <Sidebar v-model:visible="sidebarState" :baseZIndex="1000" ...

Achieving functionality with a fixed header

I am struggling with the scroll functionality and smooth transition in my code for a sticky header. The scroll doesn't work smoothly, especially when the fixed header is activated. The #top-nav-wrapper barely scrolls as expected: <script> $(doc ...

Is there a way to automatically calculate the sum of two boxes and display the result in a separate

I am working with two boxes: one is called AuthorizedAmount and the other is called LessLaborToDate: <div class="col-md-6"> <div class="form-group"> <label>Authorized Amount</label> <div class="input-group"> & ...

Attempting to input a parameter into a personalized directive

Currently developing a small search application using Elasticsearch and AngularJS. The app consists of 2 pages: home and results page. However, I am encountering an issue with my custom search directive where I need to pass the value of a service into it. ...

JavaScript and HTML with Node.js

Exploring the world of HTML, running smoothly with a static IP address 192.168.56.152 using apache on the host computer. <!DOCTYPE html> <html > <head> <title>OnlinePage</title> <meta charset="utf-8"& ...

Can someone please provide me with a Javascript code snippet that accomplishes the same function

<script> document.addEventListener('DOMContentLoaded', function () { const menuItems = document.querySelectorAll('.headmenu li'); menuItems.forEach(function (menuItem) { menuItem.addEventL ...

Discovering the children of the target DOM element in React

Imagine you have an HTML unordered list that you want to turn into a React element: <ul id="mylist"> <li><a href="">Something</a></li> <li><a href="">Another thing</a></li> <li><a ...

JavaScript code for opening and closing buttons

I created a button that successfully opens, but I am struggling to figure out how to close it. Can someone assist me with this? Additionally, I have one more query: How can I remove the border when the button is clicked? document.getElementById("arrowb ...

Is there a way to conceal my button menu when printing?

I'm trying to create a button that prints out the current page, but I want to hide my menu of buttons (which is contained inside 'mainMenuDiv') when I go to print. However, so far, all my attempts have resulted in an error saying 'uncau ...

Encountering the error message "org.openqa.selenium.JavascriptException: javascript error: eval is disabled" when attempting to conduct accessibility testing with Axe-Core

My current project involves conducting accessibility testing using the Axe-Core tool with Java as the programming language. While I have successfully obtained results from many pages in my application, I encountered an error on one specific page: org.ope ...

Encountering a challenge in Angular 8: Unable to locate a supporting object matching '[object Object]'

I am having an issue trying to retrieve the Spotify API from the current user's playlists. While I can see it in my console, when I attempt to insert it into HTML, I encounter the following error: ERROR Error: Cannot find a differ supporting object ...

Tips for enhancing the refresh rate of OpenLayer Vector layers with 200 elements

I am working on developing a timemap that combines the jquery-ui slider component and the OpenLayer library. I am utilizing the Vector layer of OpenLayers with Rules style for this project. map = new OpenLayers.Map('map', {maxResolution:'a ...

transferring a JavaScript variable to a different PHP page with JavaScript and retrieving the data

How can I pass a JavaScript variable to another page? Here is the code snippet provided: Code ` $('#disInfo').on('click','tr',function(){ var obj = $(this); var result= obj.find('td').eq(1).html(); wi ...

"Narrow down the object's properties based on an array of specified property

So here's the situation: I have an object that looks like this var obj = { name1: somevalue1, name2: somevalue2, name3: somevalue3} Followed by an array var arr = [name2, name3] Both of these are dynamically generated. What I need to do is filte ...