Utilize jQuery to toggle the visibility of a div depending on the numerical quantity input

I would greatly appreciate any assistance with this request, please.

Here is the input that I have:

<input onchange="UpdateItemQuantity(this.value,1284349,0,10352185,2579246,'','AU'); return false;" type="number" class="form-control cart-quantity" min="1" value="1"/>

If the input value is increased to 3 or more, I would like a message to be displayed in a div below.

Project Background: Customers will add items to their cart and if they reach 3, they will receive a free product

Is this feasible?

Thank you in advance!

Answer №1

To implement this feature in your website, start by inserting the following code into the html section to create a div below the input:

<div id="free-product"><p>You have a free product</p></div>

In your CSS stylesheet, add the following styling:

#free-product {
    display:none
}

Next, within your UpdateItemQuantity function, include the following code snippet:

if(this.value > 2) {
     $('#free-product').show();
}
else{
    $('#free-product').hide();
}

UPDATE:

If you are able to modify the UpdateItemQuantity function (as mentioned in the comments), consider adding an event listener to the element. Assign an id to your input and use the following script:

if($('#your_input_id').val() > 2 ) {
     $('#free-product').show();
}
$('#your_input_id').on('change',function(){
    if($(this).val() > 2 ) {
        $('#free-product').show();
    }
    else{
        $('#free-product').hide();
    }
})

View Demo: https://jsfiddle.net/w8vwsx81/

Answer №2

In this demonstration, the functionality and operation of the concept are showcased.

$(function() {
    $('.quantity').on('input', function() {
        var states = ['hide','show'],    //potential states of the freebie div
            i = +this.value > 2 ? 1 : 0; //determining index of the states array
        $('.freebie')[states[i]]();      //example $('.freebie')['hide']() is equivalent to $('.freebie').hide()
    })
    .trigger('input');
    
    $('.increase,.decrease').on('click', function() {
        var diff = $(this).is('.increase') ? 1 : -1; //deciding whether to decrement or increment
        $('.quantity').val(function() {
            return +this.value + diff; //decrement or increment
        });
        $('.quantity').trigger('input'); //manually triggering the 'input' event or any other event that shows the freebies div
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button type="button" class="decrease">-</button>
<input type="number" class="quantity" value="0">
<button type="button" class="increase">+</button>
<div class="freebie">
    <img src="http://momsneedtoknow.com/wp-content/uploads/2009/01/freebies.gif" />
</div>

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

Utilizing Ajax for populating a cascading selection menu

Currently, I am in the process of dynamically changing the options in a drop-down list using ajax and jquery by fetching data from a database. With jquery, I can easily clear and add new options to the drop-down. However, my issue arises when attempting to ...

How to Add a Responsive Inset Transparent Overlay to Images without Using JavaScript?

My goal is to create a unique overlay effect on images of different sizes within a Pinterest-style fluid grid layout. The outer border width and inset 'border gap' will remain consistent, while the images themselves need to dynamically adjust. C ...

The Kendo element's DataSource becomes null if accessed outside of a function

There is an issue with the behavior of Kendo MultiSelect's dataSource that I have noticed. When I trigger a function through an event on an element, such as a button click: function fillForm() { var ms = $("#selector").data('kendoMultiSelec ...

Utilizing the Google Translate API within an ASP MVC framework to translate a div's content from English to Arabic

Currently, I am working on a small project that involves two divs: one for English and another for Arabic. Despite creating the project, I am encountering an issue with getting the translation from English to Arabic. Below is the code I have attempted, but ...

Tips for formatting angular text sections

Within the scope of a controller, I have a status variable. After a successful rest PUT request, I add a message to this JavaScript variable. This message is displayed in my template using the {{status}} Is there a way to customize the styling of this mes ...

Restricting the number of checkboxes using jQuery depending on which radio buttons are selected

Having an issue with checkboxes on a webform. The user must choose options for a purchase by selecting checkboxes and radio buttons. There are 2 radio buttons, numbered 1 and 2, representing the quantity of the product to be purchased. Following those are ...

Getting the chosen value from a dropdown menu on form submission using PHP

How to Populate a Combo Box from a Database in PHP? <td>Item Name:</td> <td><select name="items"> <option value="0" selected="selected"> Choose</option> <?php while($row = mysql_fetch_ass ...

I can't find my unit test in the Test Explorer

I'm currently working on configuring a unit test in Typescript using tsUnit. To ensure that everything is set up correctly, I've created a simple test. However, whenever I try to run all tests in Test Explorer, no results are displayed! It appear ...

Is it possible to retrieve information from a MySQL database by utilizing Bootstrap and Ajax with the selected ID?

I am currently working on two php pages. The first php page contains a table with records that can be edited using a Bootstrap Modal, while the second php page processes updates to the MySQL database. Below is the code for the first php page (edit_account ...

Unable to transform Table layout into DIVs

My form for data input consists of labels with inputs, but the labels are localized so I am not sure about the length of text for different languages. To tackle this issue, I initially used a table layout: <table> <tr> <td> ...

Using the KnockOut js script tag does not result in proper application of data binding

Being new to knockout js, I found that the official documentation lacked a complete html file example. This led me to write my own script tags, which initially resulted in unexpected behavior of my html markups. Strangely enough, simply rearranging the pos ...

View the picture directly on this page

Currently, I am in the process of creating a gallery and I would like the images to open on top of everything in their original size when clicked by the user. My expertise lies in HTML and CSS at the moment, but I am open to learning JavaScript and jQuery ...

Is it possible to utilize a variable in determining the order of operations?

I'm currently developing a simple calculator script using a combination of HTML and PHP. The concept involves inputting two numbers and selecting the operator to perform the calculation. HTML Code <html> <head> <title>Calculat ...

Initiate a click event on an anchor element with the reference of "a href"

I'm currently facing an issue with clicking a href element on my HTML website. The click event doesn't seem to be triggered. CODE: HTML CODE: <div class="button" id="info" ></div> <ul id="game-options"> <li><a ...

The browser fails to implement styling prior to a demanding workload

Is it possible to refresh a section of my webpage that contains numerous DOM elements? My ideal approach would be to hide the section using visibility: hidden;, then redraw it, and finally set visibility back to visible;. However, I've encountered an ...

Sending Rails form data to a JavaScript click event

I'm currently working on a user profile form with a "Submit" button that triggers some client-side validations before proceeding. In my code, I've set up an intercepting click event on the submit button to run the validations and then proceed wi ...

Changing color when mouse hovers using Jquery

Currently, I am in the process of converting a flash ad into an html5 ad. I found this demo here, and I would like to replicate the mouse hover effect showcased. When the cursor hovers over the details text in the banner, the entire background changes to ...

Challenges related to maintaining a webpage's content using AJAX

I have a form that I want to use to send and insert the values into my database. After the values are inserted, I would like to clear the input fields of the form and display a success message. Here's an example of how I would like it to look: Clear ...

"Fixing the position of a div on the Samsung Galaxy S8 navigation bar to

Here is the HTML code I am working with: <div class="app-bar"> <a href="#'>icon</a> <a href="#'>icon</a> <a href="#'>icon</a> <a href="#'>icon</a> </div>' ...

jquery dialog box displaying a webpage

I am looking to implement a feature where, upon clicking the edit link in the last column of my dataTable, a separate page for editing should open within a jQuery dialog box. The goal is to seamlessly submit data while providing a user-friendly experienc ...