Leverage JavaScript to display images based on the class of the element

I am looking to display specific logos based on a user's search for a product within a certain category. Initially, the logo image element is hidden (display none) and will be assigned a class corresponding to the category ID. The category ID will determine which logo should be displayed. Here is an example of HTML code for one logo:

<li id="show-logo" style="display: none;">
    <!-- Nike Logo -->
    <img src="/images/logos/nike.jpg" class="cat_2">
</li>

Therefore, I would like my function to work in the following way:

showPartnerLogos(element, cat_id);

function showPartnerLogos(element, cat_id) {
    // Show the images with the corresponding class cat_id for that element
}

While I have not yet written any code due to my limited JavaScript skills, a similar function is currently being used to display a modal when a user searches for a product:

$('#modal .spinner').spin('flower', 'black');

// Loop through all selected categories
$("#form_buy input[name='buy_product']:checked").each(function() {
    $('#modal').find('.cat_' + $(this).val() ).show();
});

setTimeout(function() {
    document.location.href = $(form).attr('action');
},0);

This existing function displays the logos in a modal; however, I aim to achieve the same effect by displaying the logos directly on the page.

Answer №1

Give this a try

function displayLogos(element, categoryID) {
// Display images with the specified class categoryID for the element 
$(element + '.' + categoryID).show();
}

This function will show all img elements with the class provided in the categoryID.

It is possible to hide any type of element by passing the element's tag name as the parameter instead of just showing img tags.

function displayLogos(categoryID) {
// Display images with the specified class categoryID for the element 
$('img.' + categoryID).show();
}

Please inform me if this solution was helpful.

Answer №2

Here's a brief example of how it would work:

function displayCompanyLogos(element, category_id) {
    $(element+"."+category_id).show();
}

This function should be used like this:

displayCompanyLogos("img", "category_1");

This will display all image elements that belong to the category_1 class. The key is using the selector "img.category_1" in jQuery.

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

Is it possible to have code highlighting and intellisense features while typing within backticks in Visual Studio Code?

I am looking for a way to enable code highlighting and IntelliSense within backticks (``) in a .ts file in Visual Code. For example: let html =`<div><span>Hello</span></div>`; If you have experience with this feature in Atom or ar ...

Error message: The Liferay JavaScript Function has not been defined

I am a newcomer to Liferay and have been attempting to utilize Ajax within my scripts, but unfortunately, the code does not seem to load correctly in the browser. I even tried testing it by simply adding an alert. Every time I try, I encounter the "functi ...

Modifying data attribute values with jQuery

Hello there, I have a question regarding the following HTML element: <li class="total_payment" data-basetotal="0.00"> <span> <h5>Total</h5> </span> </li> I am trying to update the data-basetotal value f ...

Unable to alter the protocol option of the request object in SailsJS

Currently, I am utilizing a library that relies on the req.secure option to proceed without any errors. However, my application is deployed on Heroku and I have implemented custom middleware to check the "x-forwarded-proto" header and set req.secure as tru ...

In what scenarios would you choose to use "class" instead of "id"?

I am currently utilizing Bootstrap to style my table, which serves as a basic to-do list. My intention is to insert a form input into one cell and place a check button in the cell adjacent to it on the right side. To achieve this, I plan to utilize id="che ...

Is there a problem with renaming files using the multer module's filename options?

I can't figure out why this isn't functioning as intended. The file upload feature is operational, but the generated name consists of a long string like 04504a8b6c715f933110c8c970a8f6ad. What I need is for the filename to include the original nam ...

What causes the shift in the position of the div element?

I have been struggling with a problem in my code. Whenever I hover over this element, it rotates as intended but it also changes its position. I can't figure out why this is happening. Can someone please help me debug this? I would greatly appreciate ...

Struggling with a Bootstrap v5.0.2 Modal glitch? Let's dive into a real-life case study to troub

I am seeking assistance with a problem that I am encountering. I am currently using Bootstrap version 5.0.2 (js and css). Despite coding all the required parameters, I am unable to make the modal functionality work. I have been trying to figure out what&ap ...

Organizing tasks using jQuery queue system

I've been searching all evening and still haven't found the answer to my question. How can I queue up different actions in jQuery? People keep saying to use the callback method, but that isn't working for me. My goal is simple - I want to h ...

Using node.js for synchronous callbacks in node.js programming

I am new to node.js, and from what I've gathered, each callback creates a new event that can be executed in parallel. For instance, consider the following code with a callback: function testFunction(var1){ s3.head(var1, function(data, err){ ...

What is the method for determining if a point lies between two others?

I created a small function that seems to have a glitch... function determineIfPointIsBetweenTwoOtherPoints (pointA, pointB, pointToCheck) { var vectorAB = new THREE.Vector3(); vectorAB.subVectors(pointA, pointB).normalize(); var vectorBA = ne ...

The challenge of executing JavaScript in the correct order

I am facing an issue where 5 always prints before 4 in my code snippet below. I expected the callback to postUsers within a return statement from matchAgainstAD to wait for the completion of the for loop and ad lookup before returning. What is the simple ...

Introducing a fresh new feature incorporating various div elements through the power of JQuery

Why am I unable to retrieve the text of the newly added child divs? I am using JQuery to dynamically add these elements. Here is my sample fiddle: JSFIDDLE Could someone please guide me on what mistake I made? Javascript Code: var counter = 0; $("butto ...

show an interactive table with 2 columns for easy printing

My table is generated dynamically to display the work done by employees within a specific time period. Each work item is listed as a new row with 4 columns containing information on time spent, costs, etc. The data looks like this: Worker1 mailservice | ...

What is the best way to utilize JQuery AJAX to send XML data for a delete request?

I am having trouble sending XML type data to the backend using jQuery and AJAX as a DELETE request. When I do this, I receive an empty array from the backend's request body. How can I properly send the ID? Below is the code I am using: function delet ...

Visibility Issue with Bootstrap Modal Title and Text

I've encountered an issue with a simple model I created - when I click on it, the popup appears but the heading and paragraph are not visible. Instead, I see an empty model, which is quite strange. I've gone through the Bootstrap documentation bu ...

Transferring information between React components

After sending a request to retrieve all matching data from the database based on the answers provided in a questionnaire, I intend to utilize the data in a separate component to showcase the outcomes. <div className='row justify-content-end mt-3 p ...

Tips for sending a unique button ID to a jQuery click function

Within a table row of a dynamically generated table, I have multiple buttons each with its own functionality. My goal is to figure out how to pass the specific button ID to the onclick event of that table row when one of these buttons is clicked. $(&apos ...

Selection based on conditions and regular selection

I have successfully implemented a conditional select feature with two select boxes. However, I am now looking to enhance it by allowing the first select box to function as a normal select, displaying items based on a certain class name. Users can then use ...

What is the best way to refresh a jQuery slideshow using a jQuery show/hide event?

I am currently using jQuery to toggle the visibility of div elements on a webpage. Each div contains a jQuery slideshow. The first one functions correctly, but when navigating to the next div, the slideshow breaks (view here: ). I have been advised that I ...