Using jQuery to toggle between two elements and updating the SELECT field

<div class="r_row">
<div class="row field_currency">
    <div class="f_row"><label class="" for="currency">Currency</label></div>
    <div class="r_row"> 
    <select id="currency" name="currency" class=" select">
<option value="ron" >USD</option>
<option value="eur" selected="selected">EUR</option>
</select>
        <div class="error context" id="error_currency"></div>
    <div class="hint context" id="hint_currency"></div>
    <span class="checked">&nbsp;</span>
    <div class="fancy_select"><div class="first" value="USD">USD</div><div   class=" last" value="eur">EUR</div>
<br clear="all">
</div></div>

    <br style="clear:both">
    </div>

</code></pre>

I would like to implement a functionality using two divs

<div>USD</div><div>EUR</div>
, and with the assistance of jQuery, if I click on USD, it should update the value of the Select box and add a custom class to the USD div. Similarly, clicking on EUR should change the select value to EUR, remove the class from USD, and apply that class to EUR. How can this be achieved? Your guidance is much appreciated.

Answer №1

Here is a solution you can try:

<script>
$(document).ready(function(){
     $('.main').click(function(){
         $('.main').addClass('selectedClass');
         $('.secondary').removeClass('selectedClass');
         $('option[value=apple]').prop("selected", true);
     });
     $('.secondary').click(function(){
         $('.secondary').addClass('selectedClass');
         $('.main').removeClass('selectedClass');
         $('option[value=banana]').prop("selected", true);
     });
});
</script>

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

Receiving a notification when attempting to log in with incorrect credentials

I am currently working on an Angular login page implementation using a username and password setup. When the user enters incorrect credentials, I want to display an alert message indicating the same. Here is the HTML code snippet for the form: <form [f ...

Server encountered an unexpected T_STRING error that was not present on the localhost

While my website runs smoothly on localhost, I encounter the unexpected T_STRING error when running it on a workplace server with PHP 5.3.3. The culprit seems to be the exportXML function: eliminating this function resolves the issue. Any suggestions? I&a ...

Run JavaScript when ColdFusion page is being loaded

Within my ColdFusion page, I have incorporated multiple cfinclude template calls to bring in separate files. Before each cfinclude template call, I am seeking a way to update a javascript variable. I have attempted to achieve this by using: <script typ ...

JavaScript code for sorting a list of objects alphabetically according to a different list

I am looking to alphabetically sort a list of objects based on another list of characters and the 'name' property. Below is the sorting order I would like to use: const SortingArray = [['a','á','à','â', ...

Retrieving information from MySQL using javascript

I am trying to fetch data from a MySQL database using JavaScript. This is the script I have used to load the JavaScript: <script type="text/javascript" src="http://www.domain.de/content/entwicklung/layer.js"></script> Here is the actual scri ...

Understanding Plotly: The distinction between mode and add_trace

dat = c(1:100) fig1 = plot_ly(x = ~x) fig2 = fig1%>%add_trace(y=~rnorm(100), mode= "lines") The results displayed for "fig1" and "fig2" can be viewed here: https://i.sstatic.net/KChR7.png and https://i.sstatic.net/xhSrc.png respectively. T ...

Using Javascript with the Google Calendar API: How can I swiftly make a new event with the Quick Add feature?

Currently, I am exploring the Google Calendar API and struggling with implementing a Quick Add Event using javascript. Is it possible to achieve this task? Are there any resources or examples that can help me understand how to do it? My issue lies in the ...

Save Bson query as a Javascript log

When working with Java to create a complex MongoDB query, I often log the query before executing it: log.info("Filter: {}", queryFilter); The logged query output typically looks like this: And Filter{filters=[Filter{fieldName='FinInstrmGnlAttrbts.C ...

Effortlessly uploading large files using axios

I am currently facing an issue and I am seeking assistance. My goal is to implement file chunk upload using axios, where each chunk is sent to the server sequentially. However, the requests are not being sent in order as expected. Below is a snippet of m ...

Transitioning one element's opacity to zero while concurrently increasing another element's opacity to replace it

There are a total of 3 div elements and 3 links included in this setup. The goal is to only display one div at any given time. Upon clicking on a link corresponding to a different div, the current one should fade out while the selected one fades in seamle ...

What is the process for loading Syntax Highlighter on pages with pre tags?

As a Blogger, I often find myself in need of demonstrating codes on my blog. To achieve this, I have been using a Syntax Highlighter developed by Alex Gorbatchev. However, a recurring issue I face is that the files load on every single page of my blog, cau ...

Implementing a dynamic loading strategy for Google reCAPTCHA based on language selection

I have a unique application that requires the selection of one language out of four options (English, French, Dutch, español) in a form. Below the language selection, the Google reCaptcha is displayed. I am looking to dynamically load the reCaptcha scrip ...

Switch out a specific string within a JavaScript object

{ 1:' {person} experimenting for 1 ', 2:'{person} experimenting for 2 ', 3:'{person} experimenting for 3 ', 4:'{person} experimenting for 4 ', 5:'{person} experimenting for 5 ' } Imag ...

Do async functions in javascript function synchronously in reality?

I am currently exploring the inner workings of asynchronous code in Javascript. From my understanding, there exists a single thread in JS responsible for executing tasks in a queue. It can progress to the next task only after finishing the current one, whe ...

CSS tabs are not functioning properly as hyperlinks

Having some trouble with this website: The tabs on the site are not functioning when clicked. I've implemented a hover effect using CSS, but I'm unable to get the tabs to work properly. Please advise. Thank you. ...

Modifying the content in one form field based on the information entered in another input field

I have a scheduling application that includes a form for selecting both the departure city and arrival city. The app is designed for international travel only, so when a user selects a city from Hungary as the departure city, I want to exclude all Hungaria ...

What is the best way to retrieve response data from php using ajax in a single file?

Trying to retrieve PHP response data using AJAX has been a challenge for me. My goal is to search for a specific string in testing.txt based on user input. If the string is found, PHP should output "1", but no matter what I attempt, AJAX consistently retur ...

Extracting the video identifier from YouTube's embed code

I'm currently in the process of converting a PHP preg_match expression to extract a video ID from YouTube embed code using JavaScript. Here's what I have in PHP: $pattern = '%(?:https?://)?(?:www\.)?(?:youtu\.be/| youtube\.co ...

Avoid modifying the HTML DOM structure after utilizing the JQuery Load() method

Within an HTML Page, the Ajax Load() function is utilized to load additional HTML files into the current HTML document by targeting a specific element. When clicking on Links, the other HTML pages load correctly. However, despite the code being loaded usin ...

Is there a way to automatically remove the upload button after the file has been successfully uploaded?

I'm currently using blueimp and jquery UI to handle file uploads. My goal is to dynamically hide a specific button once a file is uploaded, and then display it again if the photo is removed. How can I achieve this functionality? https://i.stack.imgu ...