Basic jQuery Element Selection

1) Is there a way to trigger an alert only when images like 1_1.jpg, 1_2.jpg, 1_3.jpg or 2_1.jpg, 2_2.jpg, 2_3.jpg are selected and none of the others? (similar to *_1.jpg, *_2.jpg, *_3.jpg)

2) How can I shuffle the order of the image positions randomly (e.g. first: 2_1.jpg, second: 1_5.jpg, third: 1_9.jpg etc., where only the end of src (_*.jpg) should differ)?

View code at jsFiddle

HTML:

<img class="image" src="../img/Album1/1_1.jpg">
<img class="image" src="../img/Album1/1_2.jpg">
<img class="image" src="../img/Album1/1_3.jpg">
<img class="image" src="../img/Album1/1_4.jpg">
<img class="image" src="../img/Album1/1_5.jpg">
<img class="image" src="../img/Album1/1_6.jpg">
<img class="image" src="../img/Album1/1_7.jpg">
<img class="image" src="../img/Album1/1_8.jpg">
<img class="image" src="../img/Album1/1_9.jpg">
<img class="image" src="../img/Album1/1_10.jpg">
<img class="image" src="../img/Album1/2_1.jpg">
<img class="image" src="../img/Album1/2_2.jpg">
<img class="image" src="../img/Album1/2_3.jpg">

CSS:

.img {
    height:30px;
    width:30px;
    background:blue;
    margin-left:10px;
}

.selected {
    -moz-box-shadow: 0 0 7px 4px blue; 
    -webkit-box-shadow: 0 0 7px 4px blue; 
    box-shadow: 0 0 7px 4px blue;
}

JQUERY:

$( ".image" ).click(function() {
  if($(this).hasClass("selected"))
      $(this).removeClass("selected");
  else
      $(this).addClass("selected");  
});

Answer №1

1) Is there a way to receive an alert when I choose either 1_1.jpg, 1_2.jpg, 1_3.jpg or 2_1.jpg, 2_2.jpg, 2_3.jpg and none of the others are selected? Perhaps using a pattern like *_1.jpg, *_2.jpg, *_3.jpg?

You can gather your img elements into an array with $("img"), then create conditions based on both the src and class attributes within this array.

2) How can I randomly arrange the order of images (e.g. first: 2_1.jpg, second: 1_5.jpg, third: 1_9.jpg, etc., where only the end of the src (_.jpg) differs)?

To achieve this, collect your img elements in an array using $("img"), shuffle the array, and then use append to add the elements to a container. You can refer to this example for guidance.

Answer №2

1) When it comes to selecting images, the process is pretty straightforward. An alert will pop up when image 1_1 is selected, making it easy to work with the rest.

//Alert pops up when any image is selected.
$(document).ready(function() {
  var selectedImgsArr = [];
   $("img").click(function() {
       //Alerts the name of the image.
       var src = $(this).attr('src');
       if(src="../img/Album1/1_1.jpg") {
           alert("1_1 selected");
       }
   });
});

2) To randomize an array containing image names and then shuffle them for printing with JQuery, you can create a function. Check out here for more information.

Best of luck with your project!

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

Translating from a higher-level programming language to a lower-level programming language

Is compilation effectively the transformation of high-level programming languages (HLL) into machine code or low-level language? If so, why is TypeScript (a HLL) compiled to JavaScript (also a HLL) instead of being compiled to a low-level language? ...

Is it common for content to duplicate when using the 'position: fixed' property in CSS media print?

Consider the following div element: ... <div id='section-to-print'>CONT /*Content*/ </div> ... Accompanied by this CSS code snippet: @media print { * { -webkit-transition: none !important; ...

How come the max-width property is not functioning properly on tables in Internet Explorer 7?

Is there a reason why the "max-width" property does not seem to have any effect on tables in Internet Explorer 7? While it works fine on other elements, when applied to a table, the max-width rule is completely ignored. <!DOCTYPE html PUBLIC "-//W3C//D ...

Defense Against Altering POST Values via Browser Developer Tools

   Having searched extensively, I have been unable to locate an answer to my question...or perhaps I am not phrasing it correctly in my searches. Anyways, below is my question and description: As I develop a web application using Django, Javascript, H ...

Within the ASP.NET Framework 4, it is necessary to enable the Listbox control to trigger an event when a user double

Currently, I am developing a project in ASP.NET 4. I have encountered a situation where the regular ListBox in version 4.5 includes an attribute named OnMouseDoubleClick. However, since I am using ASP.Net 4, I am looking for the best approach to replicat ...

Investigate the presence of a vertical scrollbar using JQuery or JavaScript

Within an HTML report, I have applied the style "overflow:auto" to allow for dynamic data filling. I am now facing the challenge of detecting whether a vertical scrollbar exists within the report. After scouring various forums for solutions, I came across ...

Each time the Jquery callback function is executed, it will return just once

Whenever a user clicks on the "customize" link, I trigger a function to open a dialog box. This function includes a callback that returns an object when the user clicks "save". The problem is, each time the user clicks "customize", the object gets returned ...

AngularJS enhanced dropdown navigation bar built using Bootstrap styling

I'm currently learning how to implement Bootstrap, but I've run into an issue. I created a dropdown in the navigation bar, but when I clicked on it, the dropdown didn't collapse as expected. The class="dropdown" didn't change to class= ...

Mobile Bootstrap4 NavBar Bug Fix

I am currently working on coding a website with Bootstrap. The navbar functions perfectly on desktop, but when accessed via mobile device, the button containing all the links does not work. Here is a picture for a more accurate description Below is my HTM ...

css displaying drop-down menu when hovered over

I've been trying to create a link that, when hovered over, displays a list of options. I've managed to make it work in Firefox and Google Chrome, but it doesn't display at all in Internet Explorer when hovering over the link. I'm also ...

What is the correct way to invoke a function expression that is nested within another function?

Is it possible to call a function expression that is nested within another function in JavaScript? let x = function() { //perform some tasks let y = function() { console.log('Greetings Earthlings!') } } x(); ...

What strategies can be employed to troubleshoot CSS design problems that exclusively occur in the production environment?

My Node.js based URL shortening project can be found on GitHub at this link. It's deployed on Vercel, but I've been experiencing some CSS design discrepancies on the Pixel 7 device while using the Brave browser. Specifically, I'm having issu ...

What could be the reason for the absence of this Javascript function in my attribute?

I have been working on an app using electron, and I have a function that successfully adds tabs to the app. The issue arises when I try to add tabs via JavaScript with the onclick attribute - they show up as expected but do not execute the code to hide and ...

Tips for utilizing li tags to mimic the functionality of a table

I am looking to organize a list of li tags within a ul in a specific order, similar to a table layout I have created. The table <style type="text/css> .tg {border-collapse:collapse;border-spacing:0;width: 300px; height: 300px;} .tg td{font-fami ...

When using jQuery's `.click()` method on an element to collapse it with the 'show' parameter set to false, the disabling action will only take effect after the document

When you first run the program and click anywhere on the body, it activates the collapse element. I want it to only collapse the accordion on click, not show it immediately. Currently, it will deactivate only after it is hidden once. HTML <!DOCTYPE ht ...

What is the correct way to invoke a function from the reducer/actions within this specific scenario?

There seems to be an issue with the action/reducer I am attempting to call. It appears that the function is not being read correctly when called. The problem lies within the deleteWorkout function. I've attempted to use mapDispatchToProps and have al ...

Javascript timer that counts down using cookies for storage

Seeking assistance - I am in search of a solution to create a javascript countdown timer that utilizes cookies. I need the timer to remain consistent even when the user refreshes the page, until a specific time has elapsed. This is for an online examinat ...

Issues with setSelectionRange functionality in JavaScript unit tests leading to unexpected behavior

Currently, I am utilizing the qunit framework to perform unit testing on interactions within an HTML element that has been dynamically created using jquery (specifically, var $textarea = $('')) in Chrome. Below is the code snippet I am employing ...

Bidirectional data-binding in AngularJS for submitting form data via POST requests

Incorporating angularjs into my project, I am faced with the challenge of dynamically populating a form and then submitting the data via POST to a server. Initially, I set up a data variable in my controller for the purpose of later POSTing it: $scop ...

What causes jquery to malfunction when making an ajax call?

UPDATE #2: I am experiencing a similar issue to this one, although it is not related to WP: . How can I implement this with my scripts? UPDATE: The main problem arises when the page is initially loaded, everything works smoothly and ajax is not triggered. ...