Guide on displaying a paragraph with jQuery depending on two separate select options

When a user selects "Option 1" and "Option a," I want to display one thing, and for "Option 2" and "Option c," show something else...and so on.

I've been experimenting with this functionality on https://jsfiddle.net/xw6t3gL4/8/

Thank you!

$('#first').on('change', function() {
  if (this.value === "01")  {
    $("#one").show();
  } else {
    $("#one").hide();
  }
});

$('#second').on('change', function() {
  if (this.value === "a") {
    $("#one").show();
  } else {
    $("#one").hide();
  }
});
p {
  display: none;
}
<div data-role="fieldgroup">
  <select id="first">
    <option>ONE</option>
    <option value="01">Option 1</option>
    <option value="02">Option 2</option>
    <option value="03">"Option 3</option>
  </select>
</div>

<div data-role="fieldgroup">
  <select id="second">
    <option>TWO</option>
    <option value="a">Option a</option>
    <option value="b">Option b</option>
    <option value="c">Option c</option>

  </select>
</div>


<p id="one">
  1
</p>
<p id="two">
  2
</p>
<p id="three">
  3
</p>

Answer №1

What do you think of this solution?

$('select').on('change', function(){
  var select1 = $('#first').val();
  var select2 = $('#second').val();
  $('#one, #two, #three').hide();
  if (select1 === "01" && select2 === "a") {
    $('#one').show();
  }
  if (select1 === "02" && select2 === "b") {
    $('#two').show();
  }
  if (select1 === "03" && select2 === "c") {
    $('#three').show();
  }
});

Click here to view the code in action.

Answer №2

One way to update a single div's content is by using the values of the inputs. Check out this example

An alternative approach would be to let PHP handle the values and generate the desired output:

$('#output').load('process_page.php?input1=' + $("#first").val() + '&input2=' + $("#second").val());

Take a look at another demo here

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

Differences between Bootstrap 5 gutter and margin

I have a question that I'm struggling to understand. I've been looking at the Bootstrap v5 documentation and noticed a lot of changes from v4, particularly regarding gutters. Could someone please clarify the difference between using gutters and ...

Adding an image from JavaScript to a .pug file

I am a beginner in web development and I am currently trying to learn how to use JavaScript and .pug. I have created a variable named campgrounds with image files. router.get('/campgrounds', function (req, res) { var campgrounds = [ { ...

Utilize a map image as a texture on a plane within a personalized shader in THREE.js

I'm currently facing an issue where I need to load two images as textures, blend between them in the fragment shader, and apply the resulting color to a plane. However, I am struggling to even display a single texture properly. My process for creatin ...

Looking to retrieve the coordinates of an element following a CSS3 translation using JavaScript?

I came across this issue in two different variations on stackoverflow, but unfortunately the proposed solutions didn't work for me. My problem is that I need to translate an item, but when I try to retrieve its position with obj.style.left or obj.off ...

Exploring JSON data using Jquery

My goal is to extract data from a JSON feed and create two arrays, then calculate the average value of both arrays. The process starts by creating arrays as follows: $.getJSON('jasonfile.json', function(rawdata) { var array_one = []; var arr ...

The behavior of JavaScript execution when typing in a text field does not align with the expected sendKeys method

Whenever I run my tests on a Linux machine, I encounter an issue with the way text is typed using the following code snippet: visibleElement.clear(); visibleElement.sendKeys("I am running on linux machine"); Instead of typing the text correctly as expect ...

Loop through each row of the table and place an image in designated cells

I have a table with multiple rows and I am trying to dynamically insert images into specific rows based on certain criteria. Currently, I have code that changes the background color of these rows based on dates but I am struggling to figure out how to add ...

Challenges with navbar and CSS alignment in Internet Explorer

Looking for some assistance with a HTML/CSS issue... I've created a new menu using the following CSS and code, but I'm encountering a problem with extra space between lines of text within the menu (specifically with the 'big' and &apos ...

Creating a registration and authentication system using firebase

When using Google authentication with Firebase, is the Signup and Login logic the same? I am creating a page for user authentication but I'm unsure if I should have separate pages for signing up and logging in. This confusion arises from the fact that ...

What could be the reason why this LESS CSS is not taking effect?

Why won't this stylesheet load properly? The desired background color is supposed to be similar to cadetblue. You can view my page with the linked home.less.css at: ...

Unclip and clip back element when the hash changes

I am currently in the process of understanding how to properly use detach(); and could use some clarification. My goal is to detach an element that has a dynamically added attribute when the document has a specific hash, and then reattach (?) the element ...

When using the `:hover` pseudoclass, it appears that it does not apply to the `<p>` element, but it does work

While hovering over the <a>, I noticed that it changes color correctly. However, when I hover over the <p>, nothing seems to happen. It's puzzling why the <p> does not change color upon being hovered over. If I switch the selector t ...

The GitHub-hosted package encounters a failure during the npm publish process

Last week everything was running smoothly, but now I am encountering an error and not sure what went wrong. (Apologies for the formatting as there are many lines of log) Running Node version: v10.15.3 Using npm version: 6.4.1 After executing npm publish ...

Use Highcharts gauge to display data retrieved after making a $.getJSON request

Struggling to integrate data retrieved from a PHP script into a Highchart gauge graph. The JSON result is correct, but setting it into the gaugeOptions variables is proving challenging. Specifically, I want to display the PHP values in #container-C_2. Set ...

Is it possible to pass variables into a factory function?

I have a factory that is a wrapped $resource object and I am trying to include a custom header for http authentication. However, I am struggling to figure out how to pass data into the header. app.factory('SomeFactory',['$resource', fu ...

What could be causing my bootstrap-switch to malfunction?

Here is the snippet of code I am working with: jQuery.each($('.onoffswitch-checkbox'), function(i, slotData) { console.log($(slotData).bootstrapSwitch('state')) }) <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1 ...

Having difficulty customizing the color of bullet points within a list

I've been attempting to update the color of the list points without success. Can you help me troubleshoot? <div class="mobile-menu" id="mobile-menu"> <div id="mobile-menu-links"> <h4>General:</h4> ...

What is the best way to reindex dynamically added rows in jQuery?

I've been searching everywhere, but I can't seem to find the solution to my problem. I have a table where rows are dynamically added based on an index. So, when I click on a link in row 5, I want to add a new row at index 6. The issue arises when ...

Leveraging CSS selectors and combinators such as (*, ~, >, <, +)

Working with CSS selector symbols has been a challenge for me as I strive to create intricate element selectors. Currently, I find myself at a standstill struggling to combine certain selector symbols together. For example, I am attempting to craft: body ...

Guide to including a promise.map (bluebird) within a nested promise chain

I'm currently using a chain to control flow and struggling to make promise.map within step2() wait for all generated promises to be resolved. Here's a visual representation of the flow I aim for: step1() step2() step2() ste ...