Problems with select tag change events

I encountered an issue with select tag onChange events. When I select a value from the select tag, it should display in a textbox that is declared in the script. It works perfectly when I remove the class "input" from the select tag, but I prefer not to remove the class. Can someone assist me with this?

Html

<select class="input" name="month" class="style4">
   <option> Product  </option>
   <option> Product1 </option>
   <option> Product2 </option>
   <option> Product3 </option>
   <option> Product4 </option>
</select>

<input type="text" name="No_Inst" id="No_Inst"  >

Css

.input {
    box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
    border: 1px solid #999;
    border-radius: 5px;
    padding: 10px 12px;
    background: #fff;
    font-size: 14px;
    color: #000;
    box-sizing: border-box !important;
    -moz-box-sizing: border-box !important;
    outline: none;
    margin-bottom: 25px
}

js

$(document).ready(function(){    
$('.style4').change(function(){
  var a = $(this).val();

   if(a == 'Product'){
        $('#No_Inst').val('')
    }

    if(a == 'Product1'){
        $('#No_Inst').val(50)
    }

     if(a == 'Product2'){
        $('#No_Inst').val(100)
    }
});
});

Answer №1

A common mistake is using 2 class attributes in your code, but a solution is to structure it like this:

<select name="month" class="input style4">

Another issue is the lack of values in your <option> tags. You can rectify this by implementing something similar to the following:

<option value="My value">Hello</option>

Answer №2

To see changes in styles, update the class from '.input' to 'select' on your select element and make sure to remove the '.input' class

Answer №3

It is not allowed to define an attribute multiple times within a single element. When this situation occurs, the first attribute specified will take precedence over any subsequent attributes with the same name. This rule can be found in detail in the HTML5 specification, specifically section 8.2.4.35 Attribute name state

If an attribute with the exact same name already exists on the token, it is considered a parsing error and the new attribute should be removed from the token.

Therefore, make sure to only set a single class attribute.

<select class="input style4" name="month">

Instead of using:

<select class="input" name="month" class="style4">

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

Error message: Fancybox unable to open PDF file - ReferenceError: href is not defined

I came across some really interesting code on a website detailing how to open a PDF within a Fancybox iframe. However, I am trying to implement this into a CSS Menu that I generated using PHP. When I try to run the code, I receive an error message in the G ...

Looking for straightforward tips on parsing CSS code

Seeking advice on how to extract rules and property/values from a .css file or <style></style>. I am not in need of an elaborate parser, as the validity of selector text, property name, or value is not important. My main focus is ensuring that ...

Is there a way to use JavaScript to automatically open a URL at regular intervals?

List of URLs in JavaScript: var websites = ['https://www.google.com', 'https://www.msn.com', 'https://stackoverflow.com']; I have an array containing multiple website URLs. My goal is to open each URL in a new tab or window e ...

Adjusting the width of columns in a flex layout using Bootstrap 4

On mobile, I am attempting to rearrange some elements in a different order. Currently, this is what I have: https://i.stack.imgur.com/fY3PQ.png Below is the HTML code: <main> <div data-color="yellow"></div> <div class="wrapper"& ...

The challenge with our unique PHP/JS Analytics Solution

Here's an illustration of the code snippet for Google Analytics: <script type="text/javascript"> var _gaq = _gaq || []; _gaq.push(['_setAccount', 'userIDhere']); _gaq.push(['_trackPageview']); _gaq.push([&apos ...

The error "Unable to create an instance of mssql.Schema" indicates that the

Seeking assistance from experienced ReactJS developers to address the issue outlined below. The code provided is based on a tutorial I was following. Despite numerous attempts, I have been unable to resolve the issue. Here is the code snippet from User.js ...

Turn off click event during scrolling

Hello, I am trying to disable the click function after scrolling, but unfortunately it's not working as expected. Any suggestions on how to solve this issue? Thank you! $(window).scroll(function() { if ($(this).scrollTop() < 200) { ...

Looking to utilize vue.js to alter the color of the <li> element when a select option is chosen

I'm a beginner in vue.js and I'm attempting to change the background color by using the select option. Despite trying the cueCardsColor method, nothing seems to be happening. <ul> <li :class="+ cueCardColor"> <sele ...

Using an array of objects to set a background image in a Bootstrap carousel using jQuery: a step-by-step guide

I have an array of items, each containing a background property with a URL to an image. Here is my array: Here is the HTML structure: <div id="myCarousel" class="carousel slide" data-ride="carousel"> <ol class="carousel-indicators">&l ...

Navigating through an array containing references to object IDs with Mongoose

In my meanjs project, I am receiving user input on the server with a specific request body structure: { transaction: { heading: '', items: [Object] }, something: {}, somethingAgain: {} } The format of the items a ...

Having trouble with moving the svg:svg element?

I'm struggling to move an svg element that is nested within another svg. I am trying to directly manipulate the x and y values, but I keep encountering a "read-only" error. I attempted to use transform instead, but it doesn't seem to have any eff ...

"Initiate file upload using jQuery ajaxForm when dropping a file onto a designated div element

I am utilizing the jquery form plugin for Ajax file upload, and below is an example of how I have it set up: <form enctype="multipart/form-data" action="upload.php" id="form" method="post"> <input type="file" name="upfile" size="44" /> ...

Updating an existing value with a cascading dropdown list

My JavaScript code dynamically populates a dropdown list called District based on the selection made by the user in another dropdown list called Department. Here is the snippet of the code: Firstly, I populate the Department dropdownlist and add a ' ...

Loop through JSON data using jQuery for parsing

Here is the code snippet I have included below, which is a part of a larger code base: <html> <head> <style> #results{margin-top:100px; width:600px; border:1px solid #000000; background-color:#CCCCCC; min-height:200px;} </style> & ...

Tips for storing arrays in AngularJS with JavaScript

I am new to using JavaScript. I have a function that stores objects in an array to an Angular model. Here is an example: function getSpec(){ debugger var i; for(i=0;i<main.specifications.length;i++){ main.newProduct.Specification= ( ...

"MongoDB's .find function functions properly in the shell environment, but encounters issues when

As a newcomer to Node Express Mongo, I decided to venture into creating my own website after following tutorials. The page I'm working on is a login page. While other people's code has worked for me, my attempt didn't go as planned. Even con ...

Using JavaScript to auto-scroll a textarea to a certain position

Is there a way to change the cursor position in a textarea using JavaScript and automatically scroll the textarea so that the cursor is visible? I am currently using elem.selectionStart and elem.selectionEnd to move the cursor, but when it goes out of view ...

When you add the /deep/ selector in an Angular 4 component's CSS, it applies the same style to other components. Looking for a fix?

Note: I am using css with Angular 4, not scss. To clarify further, In my number.component.css file, I have: /deep/ .mat-drawer-content{ height:100vh; } Other component.css files do not have the .mat-drawer-content class, but it is common to all views ...

Is there a way to gather information from a web service and neatly display it within an HTML table?

Is it possible to fetch a JSON array from a web service and save it in a variable? Consider the following table structure: <table id="myTable" border="1"></table> The table is populated using the code below: // JSON array var myData = metho ...

the sequence in which a node executes a javascript function

Trying to update req.session.cart with new values for prod.quantity and prod.qtyCount in the shoppingCart field of order.save(). The issue is that orders are being saved with default quantity and qtyCount values, even though I'm setting cartProducts[ ...