Modify the class of the dropdown and heading 2 elements if they meet specific conditions using Animate.css

Is it possible to add a class using jQuery when two specific conditions are met?

1) If the dropdown selection is either "acting" or "backstage"

2) If the membership status is set to "Non-member"

If both of these requirements are fulfilled, I would like to apply an animation to the Upgrade button by adding the Animate.css class.

I have some parts of a function ready, but struggling to combine them effectively:

($(this).val() == 'Acting' || $(this).val() == 'Backstage')

and

$("#member_status").each(function() {
  var value = $(this).text();
  if(value === 'Non-member'){
  $("#upgrade_member").css('display','block');
  }
});

  <head>
    <link rel="stylesheet" href="style.css">
    <script src="script.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.5.2/animate.min.css">
  </head>

  <body>
    <h1 id="member_welcome">Welcome to our membership site</h1>
    <h2 id="member_status">Non-member</h2>

    <select class="regular-text" name="interest" id="interest">
                <option value="not specified">Not Specified</option>
                <option value="Watching">Watching</option>
                <option value="Acting">Acting</option>
                <option value="Backstage">Backstage</option>
                <option value="Supplier">Supplier</option>
        </select>

    <div style="margin-top:20px">
    <button id="upgrade_member">Upgrade Membership</button>
    </div>

    <div id="example" class="animated infinite bounce" style="margin-top:100px;">EXAMPLE</div>
  </body>

</html>

Here is the Plunker link

Answer №1

Here's my approach:

$("#interest").change(function() {
  var memberType = $("#member_status").text().toLowerCase().includes("non-member");
  var interestValue = $("#interest").val().toLowerCase();
  if (memberType && (interestValue == "acting" || interestValue == "backstage")) {
    $("#upgrade_member").addClass("animated infinite pulse");
  } else {
    $("#upgrade_member").removeClass("animated infinite pulse");
  }
});

$(document).ready(function(){
  $("#interest").change();
});

Check out the JSFiddle demo

Answer №2

Add a specific class to your Non-member element. For instance,

<h2 id="member_status" class="myClass">Non-member</h2>

When using JQuery,

(document).ready(function () {
 function checkMembership()
  {
   var text = $('#interest :selected').text();
   If(text == "Acting" || text == "Backstage")
      {
       $(".myClass").each(function() {
       var value = $(this).text();
       if(value === 'Non-member'){
          //perform desired actions here
       }
   }
 }}})

Call the function on dropdown change event

$("#interest").change(function () {
    checkMembership();
});

If this solution works for you, kindly mark it as the answer.

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

The functionality of WrapAll is not functioning properly in React JS

$('p').wrapAll($('<div class="wrapper"></div>')); .wrapper { background: #EEE; margin: 10px; padding: 5px; border: 1px solid black; } <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery. ...

``Is there a more SEO-friendly option instead of using an iframe?

I am looking for a solution to easily share my content with other websites without the issues I currently face. Presently, I use an iframe which poses two problems: <iframe width=“540”; height=“700” frameborder=“0” src=“http://www.energi ...

Manipulating the location where a dropdown menu intersects with a button

Is there a way to adjust the drop-down menu positioning so that it aligns with the button on the top right side of the menu pop-up, rather than the top left as it currently does? Below is the code I have borrowed from an example on W3Schools. .dropbtn ...

Retrieve information from api and showcase it in html

Recently, I came across an interesting API file that caught my attention: My goal is to extract information from this API and present it on a HTML page in a visually appealing format. Despite scouring various forums, I have yet to find a solution that fi ...

Child div height exceeds parent container bounds

Take a look at this example here: http://fiddle.jshell.net/dwuZp/15/ Can you determine why the "icons" div is overflowing the height of its parent div, "logoTimeAndIcons"? To visualize the issue, try changing the height to 50%. Your assistance is great ...

In Angular, the process of duplicating an array by value within a foreach function is not

I have been attempting to duplicate an array within another array and make modifications as needed. this.question?.labels.forEach((element) => { element["options"] = [...this.question?.options]; // I've tried json.stringify() as wel ...

Conceal div elements containing identical information by utilizing jQuery

I'm dealing with a webpage that pulls in a long section of divs from another application. Unfortunately, I can't filter the divs before they appear, but I urgently need to hide any duplicate data within certain values using jQuery. The duplicate ...

Issue with division operation in HTML not functioning

I've been working on a division calculation within an HTML file, but so far I haven't had success using PHP or JavaScript. While {{ property.listprice }} is correctly displaying a number in the HTML file, the other elements are not functioning as ...

FullCalendar, showcasing real-time event creation as you select dates

I am currently utilizing fullcalendar 4 on my website and I am attempting to incorporate an indicator for when a user selects a date on the calendar. While the background color changes, it's not very visible. My aim is to replicate the functionality ...

When my script is located in the head of the HTML page, I am unable to

My goal is to make my JavaScript code function properly when I insert it into either the head or body elements of an HTML document. Let's look at some examples: First, I insert the script into the body as shown in this example (works correctly): ...

Vanishing Submit Button with CSS

In my input submit button, I have included the following code: <input class="buttons" type="button" onclick="javascript:jQuery(xxxx)" style="font-size: 12px;" value="Invite to Bid"> Additionally, there is a CSS function that adds an elegant "Go" im ...

What's the best way to incorporate a clear options icon when choosing an option in a MaterialUI select component?

I am looking to enhance the user experience by adding a clear options icon that appears when a selection is made from the list of options. <Select InputProps={{ startAdornment: ( <InputAdornment position='end'> <Cl ...

Having difficulty changing the visibility of a div element

I am currently working on a project that involves jQuery and ASP.Net. My main goal is to create a button that can hide/show a div using jQuery. Below is the code that I have implemented: <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE html PUBLI ...

Dollar Sign vs "$$()" Sparkling Protractor with "by.css()" vs "$()"

I'm a bit confused about the purposes of the $ and $$ commands. I initially thought they were just substitutes for 'by.css', but why use $$? <element id = "eId"></element> Based on the example above, I assumed that these two l ...

What are some strategies for retrying an Apollo Client query after a failure?

When working with Next.js server-side rendering (SSR), I often encounter a query failure on the first try due to the backend not being fully ready. In such cases, I would like to retry the fetch using ApolloClient. const PREVIEW = { query: MY_PREVIEW_ ...

Understanding the user's preference for text alignment and managing how the text is displayed in a text area (or text field) within a ReactJS application

My latest project is an app that features multiple text areas. Personally, I use the app with two languages - English and another language that is aligned to the right. However, the default setting has the text-areas aligning to the left. To change this ...

Ways to clear dropdown selection in Vue based on a specific condition?

I am currently developing a dropdown menu for selecting visit status options, which include "pending," "canceled," "rejected," and "approved." In the case of an approved visit status, I would like the dropdown selection to only display the options for "can ...

Shopping with Jquery just got easier! Our innovative ajax cart feature now allows you to shop and easily checkout with

Good day, everyone. I am currently working on implementing a shopping cart feature using jQuery and Ajax. The code seems to be functioning well for one product row; however, when I try to adjust the quantity for the second product row, the calculation does ...

What is the best way to deactivate all 67 checkboxes once 4 of them have been chosen?

I am looking to limit the selection of checkboxes to only 4 out of a total of 67. However, I am currently struggling to figure out how to monitor the checkboxes and determine if exactly 4 have been selected. <table class="mdl-data-table mdl-js-data-t ...

Media queries do not trigger the execution of CSS code

I am currently working on making a desktop-sized website responsive. However, I have encountered an issue where the CSS rule is being read by the browser but not executed, regardless of whether the media query rule is true or not. I have already included & ...