Dynamic Interaction between Dropdown Menus in HTML Form

A question was posed by me 2 hours ago which got resolved: Previous Question Solved

However, upon implementing it in my code, I am facing issues as evidenced here: http://jsfiddle.net/7YeL6/5/

The problem arises when only the dropdown with vehicles appears without the second one displaying colors when "Trucks" is selected.

It seems that the implementation might be incorrect. Here's my code for anyone who can help pinpoint where I went wrong:

HTML PAGE

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <link href='http://fonts.googleapis.com/css?family=Droid+Sans' rel='stylesheet' type='text/css'>
    <meta charset="utf-8">
    <title>Add Item</title>
    <link rel="shortcut icon" href="../style/kk-a.png" type="image/x-icon">
    <link rel="stylesheet" href="style_copy.css" type="text/css" media="screen" />
    <link href="style_menu/h_menu_style.css" media="screen" rel="stylesheet" type="text/css" />
    <link href="style_menu/h_menu_iconic.css" media="screen" rel="stylesheet" type="text/css" />
    <link href="style_menu/main_color_dropdown.css" media="screen" rel="stylesheet" type="text/css" />
        <script src="style_menu/h_menu_prefix-free.js"></script>
        <script src="subcategory_auto_dropdown.js"></script>
</head>

<body>
<div align="center" id="mainWrapper">
  <?php include_once("includes_admin/header_admin_template.php");?>
  <div id="pageContent"><br />
    <div align="left" style="margin-left:30px;"><a href="inventory_list.php"> « Go to Inventory List</a></div>
    <br />
    <br />
    <div align="left" style="margin-left:24px;">    
    <form action="inventory_list.php" enctype="multipart/form-data" name="myForm" id="myform" method="post">
    <table width="100%" border="0" cellspacing="0">
        <tr>
        <td width="32%" colspan="1" align="left"><img src="../style/add_item.png" width="200" /></td>
        <td width="33%" align="left">&nbsp;</td>
        <td width="35%" align="left">&nbsp;</td>
        </tr>

        <tr>
          <td colspan="3" align="left"><hr style="color:#616161"; /></td>
        </tr>

        <tr>
        <td colspan="1" align="left">&nbsp;</td>
        <td align="left"><span style="padding-bottom:10px">Category</span></td>
        <td align="left"><span style="padding-bottom:10px">Category 2</span></td>
        </tr>

        <tr>
          <td colspan="1" align="left" style="padding-bottom:10px">&nbsp;</td>
          <td align="left" style="padding-bottom:10px">
                  <select name="category" id="category">
                      <option selected value="Please Select">Please Select</option>           
                      <option value="Cars">Cars</option>
                      <option value="Trucks">Trucks</option>
                      <option value="Motorcycles">Motorcycles</option>
                      <option value="Boats">Boats</option>
                   </select>
          </td>
          <td align="left" style="padding-bottom:10px">
                  <select name="category2" id="truck" class="second">
                      <option value="white">white</option>
                      <option value="black">black</option>            
                  </select>

                  <select name="category2" id="car" class="second">
                      <option value="red">red</option>
                      <option value="green">green</option>
                      <option value="blue">blue</option>           
                  </select>
          </td>      
        </tr>
      <tr>
        <td colspan="3" align="left"><input type="submit" name="button" id="button" value="Submit +ADD"/></td>
      </tr>
      </table>
    </form>
    </div>
    <br />
  <br />
  </div>
  <?php include_once("includes_admin/footer_admin_template.php");?>
</div>
</body>
</html>

subcategory_auto_dropdown.js

$("#category").change(function () {
  var str = "";
str =  $("select#category option:selected").text();
    if(str == "Trucks"){
        $("select.second").not("#truck").hide();
        $("#truck").show();
        $("#truck").fadeIn(1000);
    }
    else if(str == "Cars"){
        $("select.second").not("#car").hide();
        $("#car").show();
        $("#car").fadeIn(1000);
    }

})

style_copy.css

/* SUBCATEGORY DROPDOWN AUTO CHANGE OPTION  */
#category2{
    display: none;
}
.second{
    display: none;

}

Answer №1

Your code is missing the jQuery library inclusion. To rectify this, insert the following line before any other script tag:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

After adding the jQuery library, ensure that your code in subcategory_auto_dropdown.js follows this structure:

 $(document).ready(function(){
    //existing JS code
 });

This section of code initiates your jQuery script when the page loads. Without it, the code will not function as intended.

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

I am struggling to make custom output for jQueryUI Autocomplete menu items function as intended

I am currently facing an issue with passing values from an object retrieved from a JSON string into a jQueryUI Autocomplete element. Specifically, I need to extract three different values from a 2-dimensional array format like this: [{1:a1, 2:b1,3:c1,4:d1 ...

The HTML code on the page does not match the code I see when inspecting it

I need to extract the cost savings and frequency of interruptions information from the table on https://aws.amazon.com/ec2/spot/instance-advisor/ using Python. After inspecting the website using Chrome's 'Inspect' feature, I discovered that ...

When integrating the React custom hook setValue into another component, it appears to be returning an undefined

I have created a custom useLocalStorage hook. When I directly use it in my component and try to update the value, I encounter an error stating that setValue is not a function and is actually undefined. Here's the code snippet: // Link to the original ...

including a "continue" option to the jplayer interface

Does anyone know how to add a next button on jplayer? I've tried using the click function, but it doesn't seem to be working. The player is set up to use ajax to fetch songs and information about them. When one song ends, another is retrieved th ...

Using JavaScript to trigger a bootstrap dropdown toggle

Working on a bootstrap 4 dropdown menu that expands when hovered over, the goal is to prevent the link from being followed on touch-enabled devices and instead toggle the dropdown. I have successfully disabled the link, but toggling the menu remains a chal ...

Tips for avoiding errors caused by chart adjustments in Angular.js

I have two different perspectives, view A and view B. In view A, I present my chart. However, upon transitioning to view B, I encounter a quick error. It seems that this error arises due to a setTimeout function not being completed before the view change o ...

Leveraging Multiple MongoDB Databases in Meteor.js

Can 2 Meteor.Collections fetch data from separate MongoDB database servers? Dogs = Meteor.Collection('dogs') // mongodb://192.168.1.123:27017/dogs Cats = Meteor.Collection('cats') // mongodb://192.168.1.124:27017/cats ...

Record Selenium requests made to REST APIs

When conducting integration tests using Selenium as the test runner and the webdriver.io JavaScript library for the Selenium API, my testing scenario involves loading an HTML page and clicking on a button. I am specifically aiming to verify whether a GET R ...

Tips on personalizing rows in an xlsx spreadsheet prior to exporting

When I receive an array of objects (dicts) as input, it looks something like the example below. const data = [ { id: 1, serviceId: 1, title: "A", model: "A", }, { id: 1, serviceId: 1, title: "T", model: "b", ...

What is the inverse of the .is() selector in jQuery?

Attempting to create a hover function using the is() selector. However, what is needed is the opposite! $('.tray .tab').is(".active").hover( ...function ); Although I tried using the not() selector, it's actually a filtering method and doe ...

Tips for creating Django URL in template with jQuery Ajax

When coding in Django, it's essential to adhere to the DRY principle and hard-code URLs in a centralized location like the urls.py file. However, when passing a URL from a Django template to a jQuery Ajax function, many developers struggle to find the ...

The issue of AngularJS function returning at an incorrect point

Can someone explain to me why my JavaScript function behaves this way? It loops through an array of 3 objects, returning true when meeting a condition in the if statement. However, it does not exit after the first true and continues looping, ultimately ret ...

Is there a way to display the JavaScript widget exclusively on the homepage or main page

How can I limit the display of a Twitter widget on my blog page to only show on the main page and hide it when visitors navigate to sub-pages or individual blog entries? ...

Is {{@index}} an even or odd number in Handlebars: A Step-by-Step Guide

I'm currently cycling through an array, and I'd like to adjust the background color of the div based on whether the current index is odd or even. Unfortunately, when I try to use {{@index}} within an if statement in Handlebars, like so: {{#each ...

Why don't I need to include an onload event to execute the setInterval() method within the script tag?

Hey there! I'm diving into the world of Javascript and I've come across this interesting code that changes an image every four seconds. Surprisingly, it's working perfectly fine even though I didn't include an onload event to execute th ...

Exploring the wonders of jquery's JSON parsing capabilities

Having trouble parsing the JSON data that is out of my control. Can anyone help me figure out what I'm doing wrong here? data.json { "img": "img1.jpg", "img": "img2.jpg", "size": [52, 97] } { "img": "img3.jpg", "img": "img4.jpg", "size ...

How to resolve undefined Axios Authorization headers in Vue.JS and Node.JS interactions?

Encountering an issue while trying to set authorization headers for my axios instance. Here is the code snippet from my Vue.JS application running on http://localhost:8080 : axios.defaults.headers.common['Authorization'] = 'Bearer ' ...

Ensure the page is always updated by automatically refreshing it with JavaScript each time it

I'm currently working on a web application that makes use of JQuery. Within my javascript code, there's a function triggered by an onclick event in the HTML which executes this line: $('#secondPage').load('pages/calendar.html&apos ...

Canvas is unable to duplicate image content

I am trying to duplicate a PNG captcha image in order to remove its transparency so that the captcha resolver (ocrad.js) function can work properly. However, I am facing an issue where the duplicated captcha image is being moved and resized. How can I ke ...

Display routes in React using the react-router package

Can I use a console command at runtime to display all routes in my application? Despite utilizing react-router, not all routes seem to be functioning properly. In Rails, you can retrieve a list of routes during runtime. ...