What is the method for presenting text based on the chosen Select Option?

I attempted to achieve this using hrefs and ids, but it did not meet my requirements.

This is the desired format:

div.selectcountry {
  margin-bottom: 10px;
  font-family: arial;
  font-size: 12px;
}
div.countrydepartment {
  font-family: arial;
  font-size: 12px;
}
<div class="selectcountry">Choose your country: <select>
  <option>Russia</option>
  <option>China</option>
  <option>Tajikistan</option>
  </select></div>

<div class="countrydepartment">Here should be a text depending on the chosen country.<br>
– Russian department: +7 (000) 000 00 00<br>
– Chinese department: +86 (000) 000 00 00<br>
– Tajikistan department: +992 (000) 000 00 00<br>
</div>

This webpage provides an example of what I am trying to achieve:

How can I accomplish this? Perhaps using JavaScript? But how? Please provide me with instructions.

Thank you for your assistance and guidance!

Answer №1

Implement event listening in jQuery for changes.

$('.selectcountry select').change(function() {
  country = $(this).val();
  //reset
  $('.countrydepartment span').removeClass('active');
  //display current country department
  $('.countrydepartment span[country="' + country + '"]').addClass('active');
});
div.selectcountry {
  margin-bottom: 10px;
  font-family: arial;
  font-size: 12px;
}
div.countrydepartment {
  font-family: arial;
  font-size: 12px;
}
.countrydepartment span {
  display: none;
}
.countrydepartment span.active {
  display: inline-block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="selectcountry">
  Choose your country:
  <select>
    <option selected disabled>--- Select Your Country ---</option>
    <option value='russia'>Russia</option>
    <option value='china'>China</option>
    <option value='tajikistan'>Tajikistan</option>
  </select>
</div>

<div class="countrydepartment">
  <span country='russia'>— Russian department: +7 (000) 000 00 00</span>
  <span country='china'>— Chinese department: +86 (000) 000 00 00</span>
  <span country='tajikistan'>— Tajikistan department: +992 (000) 000 00 00</span>
</div>

Answer №2

To effectively monitor changes in the select element, it is important to assign appropriate classes:

<div class="selectcountry">Choose your country:
    <select class="selcountry">
        <option value='1'>Canada</option>
        <option value='2'>Brazil</option>
        <option value='3'>Australia</option>
    </select>
</div>
<div class="countrydepartment">Text specific to the selected country will be displayed here.
    <div id='countrycontacts'></div>
</div>

Below is a sample JavaScript/jQuery code snippet for handling the change event:

$(function () {
    var cc = ['Canadian department: +1 (000) 000 00 00', 'Brazilian department: +55 (000) 000 00 00', 'Australian department: +61 (000) 000 00 00'];
    $('.selcountry').change(function(e) {
        var country = $(this).val() - 1;
        var contactinfo = cc[country];
        $('#countrycontacts').html(contactinfo);
    });
});

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

Display the variance in values present in an array using Node.js

I am facing a challenge and need help with printing arrays that contain both same and different values. I want to compare two arrays and print the values that are present in both arrays in one array, while also creating another array for the differing val ...

Is there a way to immobilize an object in JavaScript without resorting to Object.freeze()?

Is there a way to freeze the following object without relying on Object.freeze()? Let's find out: const obj = { a:'test', b:'Something' } ...

Convert the JSON data received from a jQuery AJAX call into a visually appealing HTML table

Utilizing AJAX as the action, I have created a search form. In the success of the AJAX request, I am seeking a way to update a specific div without refreshing the entire page. Below is my form: <?php $properties = array('id' => &ap ...

Video margins within a webview

After numerous attempts to embed a YouTube video in a WebView, I'm still struggling with a persistent small margin on the right side of the video. I've researched similar issues and attempted to address it through JavaScript adjustments without ...

An obstacle with jQuery's resize function when checking element visibility

Having trouble with my responsive navigation when resizing the browser window. Check out this simple codepen to see the issue in action... http://codepen.io/anon/pen/xbEwmJ When starting on a wide viewport (desktop view) and then shrinking the size, you& ...

Instructions for setting a default value for ng-options when dealing with nested JSON data in AngularJS

I need help setting a default value for my ng-options using ng-init and ng-model with dependent dropdowns. Here is an example of my code: <select id="country" ng-model="statessource" ng-disabled="!type2" ng-options="country for (country, states) in c ...

identifying HTTP requests originating from an embedded iframe

I have a unique scenario where there is an iframe on my page that calls a URL from a different domain. Occasionally, this URL will tunnel to a different URL than the one I specified. Unfortunately, due to cross-domain restrictions, I am unable to view the ...

Creating a dialog box in jQuery triggered by a user clicking a link

Just starting out with JQuery and curious about creating a dialog box that appears when a user clicks on a link. For example, if I click "send," I want a dialog box to pop up saying "your information has been sent!" Any assistance is greatly appreciated! ...

The entire title serves as a hyperlink, not just the text portion

Apologies if I'm making mistakes, I'm just starting out with coding and navigating this website. So here's the deal: I am trying to add advertisements to my website on the left and right sides of the centered "Logo" at the top. The issue i ...

Step-by-step guide on resolving AngularJS Issue: [$injector:modulerr]

I'm encountering an issue with Angular JS where I'm receiving the following error: jquery.js:7993 Uncaught Error: [$injector:modulerr] Failed to instantiate module application due to: Error: [$injector:nomod] Module 'application' is no ...

Adjust the carousel width on slide in Bootstrap 4

Having an issue with my Bootstrap Carousel where the width changes as it slides. The carousel starts off fine, But when it slides, I want the carousel to stay centered and maintain a fixed width. Setting a fixed width for carousel-inner works, but it n ...

Enhancing Rails forms with JQuery: A guide to updating select options

In my _form2.html.erb file, I have the following script: <script type="text/javascript> $(function () { $('#toright').click(function () { var sel = document.getElementById("left"); var len = sel.options.leng ...

Can you switch out the double quotation marks for single quotation marks?

I've been struggling to replace every double quote in a string with a single quote. Here's what I have tried: const str = '1998: merger by absorption of Scac-Delmas-Vieljeux by Bolloré Technologies to become \"Bolloré.'; console ...

jQuery popups are not being initialized properly following an ajax response

My current setup involves using a pageless solution to showcase a listing of various locations. Among these locations, there is a jQuery popup that displays additional data in a box. The issue arises when new locations are added dynamically via ajax, as th ...

Vuetify - Implementing a search feature in a table that automatically navigates to the matching row

Currently, I am working with 2 Vuetify data tables that do not have pagination enabled. Each row in the second table corresponds to exactly one parent in the first table. My goal is to be able to click on an entry in the second table and have it automati ...

What causes the scrollbar to not extend to the bottom when connected to another scrollbar via a JavaScript equation?

I have been working on a code that involves multiple scrollbars that are all linked together. When you move one scrollbar, the other two will move proportionally. However, due to differences in width, the scroller doesn't always reach the end of the s ...

The client.postMessage() function in a service worker is not being recognized by the onmessage handler within an AngularJS controller

Handling Service Worker Messages: let angularClient; self.addEventListener('message', function(event) { // store the client that sent the message in the angularClient variable angularClient = event.ports[0]; }); Service Worker Notificat ...

Changing Page Content with Ajax Post-Redirect Pattern

Just had a quick question. Can the redirected page be affected by ajax's success function? The code will provide a better explanation. $.ajax({ type: "POST", url: "/admin/done", data: { ...

Eliminate any blank spaces in the string before comparing the option value to determine whether the DIV should be

I am trying to create a function that hides or shows a specific DIV based on the option value selected from the product. Due to automatic addition of option values by Shopify, some options have more than one word with spaces in between. Since I cannot chan ...

Obtain the identifier for the Summernote component

I need a solution for retrieving the element ID of multiple summernote fields on a page when performing an action onblur. Currently, my attempts to capture the elements ID result in it being returned as undefined Sample HTML code: <div class="summerno ...