What is the process for changing an image on a webpage based on a dropdown list selection?

When I came across the site , I was impressed by their feature that allows users to select their currency. By changing the currency, the symbol beside the amount entered also changes accordingly.

I am interested in incorporating a similar feature on my website, but I am unsure of how to proceed. Can anyone provide guidance on how to achieve this?

Please bear with me as I have some experience in creating websites, but nothing too complex or extraordinary.

Answer №1

The website's code operates on the client-side and is quite straightforward.

By clicking the link, Javascript activates to display a selection popup div.

Upon selecting an item from the popup div, more Javascript is triggered. This Javascript alters:

  • The original div's label text for the amount field (such as the prominent )
  • The text below that denotes the currently selected currency type (which appears when the div pops up)
  • The data within the concealed form field containing the chosen currency type

...and then closes the popup div.

Edit: It seems that you also require the jQuery library to utilize the code provided in my response :) You can substitute your own Javascript code to achieve the same results, although it won't mirror the code below exactly.

Here is the code directly extracted from "view source":

The box for the amount:

<div class="amt_box bg_white">
    <label class="currency-display">$</label>
    <input type="text" name="Funds[goalamount]" value="" class="big-field"
        tabindex="1" />
</div>

The link that triggers the popup div:

<h4>Display: <a href="#" class="currency-select">US Dollars</a></h4>

The popup div:

<div class="currency currency-select-div" style="position:absolute; display:none;margin-left:45px;">
    <ul>
        <li><a href="#" class="currency-item" title="$" code="USD">&#36; USD Dollar</a></li>
        <li><a href="#" class="currency-item" title="$" code="CAD">&#36; CAD Dollar</a></li>
        <li><a href="#" class="currency-item" title="$" code="AUD">&#36; AUD Dollar</a></li>
        <li><a href="#" class="currency-item" title="£" code="GBP">&#163; GBP Pound</a></li>
        <li><a href="#" class="currency-item" title="€" code="EUR">&#128; EUR Euro</a></li>
    </ul>
</div>

The hidden form field:

<input type="hidden" id="currencyfield" value="USD" name="Organizations[currencycode]" />

The Javascript (jQuery) code that links everything together:

$('.currency-select').click(function() {
    $('.currency-select-div').show();
    return false;
});

$('.currency-item').click(function() {
    $('.currency-select-div').hide();
    $('.currency-display').text($(this).attr('title'));
    $('.currency-select').text($(this).text());
    $('#currencyfield').val($(this).attr('code'));

Answer №2

If you want to customize the text using a drop-down list, the first step is to give it a unique identifier.

<span id="currencySymbol">$</span>

Next, create a drop-down list with your desired values.

<select id="currencySelect">
    <option value="$">Dollars</option>
    <option value="£">Pounds</option>
    <option value="€">Euros</option>
</select>

Finally, you'll need to utilize JavaScript to update the text based on the selected value from the drop-down list.

document.getElementById('currencySelect').onchange = function(){
    document.getElementById('currencySymbol').innerHTML = this.value;
}

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

How does the use of <ol> with list-style-type: disc; differ from that of <ul>?

Why create a separate <ul> when visually both unordered lists and ordered lists look the same? <head> <title>Changing Numbering Type in an HTML Unordered List Using CSS</title> <style> ol { list-s ...

Why isn't v-model functioning properly in Vue?

In my current project involving an API, I encountered a problem. I utilized axios to fetch data from the API, which returned a JSON array. Each array item had a radio value that I appended to it. Despite attempting to use v-model to track changes in the ra ...

How to combine two tables in Sequelize using a one-to-many relationship

I'm currently working with two models: User and Foto. In my application, each User can have multiple fotos, and each foto is associated with only one user. My challenge lies in using the include function. I am able to use it when querying for the us ...

Creating dynamic insert SQL statements in Oracle 11G

I have been given a task to compare all columns within a schema with the trends of the same columns. To achieve this, I have created a query that generates bulk insert statements. The total number of insert statements will exceed 50K+. The SQL query I have ...

Steps to upgrading the React version within a package

Upon executing npm ls react, the following output is displayed: PS > npm ls react ├─┬ @headlessui/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cfbdaaaeacbb8ffee1f8e1fefa">[email protected]</a> │ └ ...

"The function within the Sails.js model is not properly returning a value when rendered

Within my Sails.js application, I am currently attempting to retrieve the total number of users that have been approved in my database and utilize this count variable within my view by employing the count() query. Below is the snippet of code present in my ...

CSS: Only one out of three <div>'s has the styles applied in JSFiddle

When working on my project, I encountered an issue while trying to add CSS styles to three <div> structures with unique IDs. Strangely, making small changes to unrelated CSS caused elements to go from being stylized to losing their style. If you com ...

Retrieve the link's "href" attribute and its text if they match the specified text

Is there a way to extract the href link and text from a specific type of link? <a href="google.com">boom</a> I am looking to retrieve only the 'google.com' part and the text 'boom' Furthermore, how can I filter out other ...

Is the default animation duration of Compass being ignored?

After recently updating to Compass version 1.0.16, I started setting up basic usage of the new animation features. However, I encountered an issue where setting default values for different animation settings did not take effect. This led me to hard-code t ...

Trouble with browser autocomplete/saved form during ajax requests

Searching for information on browser autocomplete and form saving can be tricky, especially when trying to find solutions related to custom autocomplete with ajax. Many developers are interested in customizing autocomplete features for various reasons such ...

Using React Native to pass parameters with drawerNavigator

Lately, I've been utilizing switchNavigator for navigating between different views in my app. However, I'm now experimenting with drawerNavigator. Passing props with switchNavigator is quite straightforward since I navigate through a button, like ...

A step-by-step guide on performing requests sequentially within a useEffect hook

My useEffect function triggers a request when changing dependencies (department and two dates fields). useEffect(() => { getFilteredRestReport({ date_start: formatDatePatchPoint(date[0]), date_end: formatDatePatchPoint(date[1]), de ...

Trouble encountered when using RxJS zip and pipe together

In my Angular Resolver, I am facing a scenario where I need to wait for two server calls. The catch is that the second server call is optional and can be skipped based on user input. This data retrieval process is crucial for loading the next page seamless ...

Clicking multiple times will impede the progress of AJAX requests

My webpage has multiple buttons that can be clicked very quickly, but it seems like Chrome is struggling to keep up with the speed? $(document).ready(function() { $('.favorite-button').unbind('click').click(function() { addLine ...

Sending Laravel 5.4 Controller Response to Blade Using Ajax (Fixing json_encode() Error with Object Parameter)

I am trying to send data from the controller to AJAX as an array, but I am encountering the following error: json_encode() expects parameter 2 to be integer, object given Below is my code snippet: function get_show(Request $request) { $i ...

Troubleshooting a problem with jQuery waypoints in a static HTML/JS

Utilizing waypoints and windows to display the panels similar to the fiddle example I have created: http://jsfiddle.net/6bMMa/1/ Everything is functioning correctly, however, I have only managed to make it work by using id numbers on the panel divs. The ...

What could be causing my CSS/Layout to alter once AJAX/JavaScript has been executed?

When the user clicks, an AJAX call is made to retrieve data from a third-party API. The frontend/layout renders correctly before this action; however, after the data is fetched and displayed in the frontend, the layout changes. Upon debugging multiple tim ...

What is the Java method for clicking on an image button in WebDriver?

After entering all the required data and clicking on the Apply Now button, it is not saving. However, when attempting to create it manually, the process completes in less than 15 seconds. Attached is a screenshot for reference. Here is the relevant HTML ...

The Font Awesome icon is not displaying on top of the Materialize CSS progress/determinate bar

I am struggling to display a Font Awesome icon on top of a Materialize CSS progress/determinate div. Unfortunately, the icon is not fully visible and part of it gets clipped or hidden. Despite trying various solutions, I have not been successful. .progres ...

Showing Stationary Pictures at Full Size in OpenLayers

I am trying to showcase static images as maps using a StaticImage layer in ol3, with the image size set at 100% in pixels. However, I am facing difficulty in ensuring that the displayed images are always the correct size based on the extent and zoom variab ...