having difficulty applying border color to input when clicked

Is there a way to change the border color of an input field on an onClick event?

In my scenario, if the 'Other' radio button is selected and a user attempts to save without entering any value in the input field, I want the border color of the input field to turn red to indicate that a value is required.

$('#nextsales3Btn').on('click', function() {
  var ratestructure = $('input[name="ratestructure"]:checked').val();  // radio button
  var otherstructure = $('#otherratein').val();

  if(ratestructure == "Other" && otherstructure == "")
  {
    $('#otherratein').css('border', 'red');
    console.log('here');
    return false;
  }
  else
  {
    console.log('all good');
  }
});

The logic above works as expected. The console displays 'here' when the radio button is checked and the input is empty. However, changing the border color of the input field does not seem to work.

I have tried the following without success:

$('#otherratein').css('border-color', 'red');

Unfortunately, this did not produce the desired outcome.

Any guidance on how to resolve this issue would be greatly appreciated?

* EDIT *

Here is an example of how the relevant HTML code looks:

<div class="form-row">
     <div class="col-md-12">
        <div class="form-group"> 
           <label id="ratestructureLabel">Required Rate Structure:</label>                                     
           <br/> 
           <div class="form-check"> 
           <input class="form-check-input" type="radio" name="ratestructure" id="allin" value="all-in"> 
           <label class="form-check-label" for="yesebiz">All-In</label>                                         
           </div>
           <div class="form-check"> 
           <input class="form-check-input" type="radio" name="ratestructure" id="allindest" value="all-inD"> 
           <label class="form-check-label" for="allindest">All-In, subject to Destinations</label>                                         
           </div>
           <div class="form-check"> 
           <input class="form-check-input" type="radio" name="ratestructure" id="allinori" value="all-inO"> 
           <label class="form-check-label" for="allinori">All-In, subject to Origins</label>                                         
           </div>
           <div class="form-check"> 
           <input class="form-check-input" type="radio" name="ratestructure" id="boforindest" value="bofOD"> 
           <label class="form-check-label" for="boforindest">BOF, subject to Origins & Destination</label>                                         
           </div>
           <div class="form-check"> 
           <input class="form-check-input" type="radio" name="ratestructure" id="pernote2" value="pernote2"> 
           <label class="form-check-label" for="pernote2">Per Note 2</label>                                         
           </div>
           <div class="form-check"> 
           <input class="form-check-input" type="radio" name="ratestructure" id="surcharges" value="surcharges"> 
           <label class="form-check-label" for="surcharges">Surcharges per tariff</label>                                         
           </div>
           <div class="form-check form-check-inline"> 
           <input class="form-check-input" type="radio" name="ratestructure" id="otherrate" value="Other Rate"> 
           <label class="form-check-label" for="otherrate">Other:</label>
           <input type="text" class="form-control ml-2" name="otherstructure" id="otherrateIn" placeholder=""> 
           </div>
         </div>
       </div>
     </div>

Answer №1

Firstly,

Explanation and Application The border attribute serves as a condensed version of:

border-width border-style (mandatory) border-color

thus:

 border: 5px solid red;

In javascript:

$('#otherrate').css('border', '5px solid red');

Answer №2

Simply adding a color may not yield any visible changes. It is suggested to experiment with the following approach:

$('#otherrate').css('border', '1px solid red')

I strongly advise utilizing CSS for this task.

Answer №3

border-color and border are two distinct properties when it comes to styling elements.

In order to change the border color of an element using jQuery, you can use the following code:

$('#otherrate').css('border-color', 'red');

However, if you want to set the complete border property with a specific color, width, and style, you should use the following code:

$('#otherrate').css('border', '1px solid red');

Answer №4

To display a border, use || instead of && because if any one condition is true. I have also assigned a class to your div that contains the radio button named Others in order to give a border to the entire div.

$('#nextsales3Btn').on('click', function() {
  var ratestructure = $('input[name="ratestructure"]:checked').val();  // radio button
  var otherstructure = $('#otherrate').val();

  if(ratestructure == "Other Rate" || otherstructure == "")
  {
    $('.otherrate').css('border', '1px solid red');
    console.log('here');
    return false;
  }
  else
  {
  //removing border
   $('.otherrate').css('border', '');
    console.log('all good');
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
 <div class="form-row">
     <div class="col-md-12">
        <div class="form-group"> 
           <label id="ratestructureLabel">Required Rate Structure:</label>                                     
           <br/> 
           <div class="form-check"> 
           <input class="form-check-input" type="radio" name="ratestructure" id="allin" value="all-in"> 
           <label class="form-check-label" for="yesebiz">All-In</label>                                         
           </div>
           <div class="form-check"> 
           <input class="form-check-input" type="radio" name="ratestructure" id="allindest" value="all-inD"> 
           <label class="form-check-label" for="allindest">All-In, subject to Destinations</label>                                         
           </div>
           <div class="form-check"> 
           <input class="form-check-input" type="radio" name="ratestructure" id="allinori" value="all-inO"> 
           <label class="form-check-label" for="allinori">All-In, subject to Origins</label>                                         
           </div>
           <div class="form-check"> 
           <input class="form-check-input" type="radio" name="ratestructure" id="boforindest" value="bofOD"> 
           <label class="form-check-label" for="boforindest">BOF, subject to Origins & Destination</label>                                         
           </div>
           <div class="form-check"> 
           <input class="form-check-input" type="radio" name="ratestructure" id="pernote2" value="pernote2"> 
           <label class="form-check-label" for="pernote2">Per Note 2</label>                                         
           </div>
           <div class="form-check"> 
           <input class="form-check-input" type="radio" name="ratestructure" id="surcharges" value="surcharges"> 
           <label class="form-check-label" for="surcharges">Surcharges per tariff</label>                                         
           </div>
           <div class="form-check form-check-inline otherrate"> 
           <input class="form-check-input" type="radio" name="ratestructure" id="otherrate" value="Other Rate"> 
           <label class="form-check-label" for="otherrate">Other:</label>
           <input type="text" class="form-control ml-2" name="otherstructure" id="otherrateIn" placeholder=""> 
           </div>
         </div>
       </div>
       </div>
       <button id="nextsales3Btn">Check</button>

Answer №5

After modifying the HTML code, I included a new class for the input field. By utilizing that class in my jQuery script, I successfully customized the border of the input element.

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

Cross-Origin Resource Sharing (CORS) verification for WebSocket connections

I am currently utilizing expressjs and have implemented cors validation to allow all origins. const options = { origin: ['*'], credentials: true, exposedHeaders: false, preflightContinue: false, optionsSuccessStatus: 204, methods: [&a ...

Issue with React component timer becoming unsynchronized with numeric input field

My number field and countdown timer are not staying synchronized. Even though I can start and pause the countdown, whenever I try to change the number value after pausing, the numbers get out of sync. The gap between the two values keeps growing as time p ...

Refresh the table following the removal of an item

I am currently working on displaying server data in a table. The delete function is functioning properly, but the deleted item only disappears from the table after refreshing the page. Is there a way to trigger a re-render of the component after deleting a ...

Concealing items in a loop using Javascript

I need assistance with this issue. I am trying to hide text by clicking on a link, but it doesn't seem to be working correctly. Even though I tried the following code, I can't figure out why it's not functioning as expected. Is there a diff ...

Determining the Clicked Button in ReactJS

I need help with a simple coding requirement that involves detecting which button is clicked. Below is the code snippet: import React, { useState } from 'react' const App = () => { const data = [ ['Hotel 1A', ['A']], ...

Why is the editor not displaying the correct line number when the enter key is pressed repeatedly in JavaScript?

I am currently working on developing an editor using JavaScript. My goal is to display a line count (e.g., 1, 2, 3) whenever the user presses 'Enter' to change lines. I have successfully managed to count the line changes upon pressing 'Enter ...

Eliminate the preset padding on the left and right sides of the container-fluid in Bootstrap 5

How can I remove the default padding-left and padding-right in a container-fluid using Bootstrap 5? I don't want to use "!important" in my CSS file. I have already tried disabling the default padding with Chrome dev tools but it didn't work. Any ...

Using Angular 5 to link date input to form field (reactive approach)

I'm encountering an issue with the input type date. I am trying to bind data from a component. Below is my field: <div class="col-md-6"> <label for="dateOfReport">Data zgłoszenia błędu:</label> <input type="date" formC ...

Learn the process of adding transformation to an SVG path element

I have an SVG image loaded in my HTML file and I am trying to rotate one of its path elements, but I'm having trouble figuring out how to do it. The code snippet provided below is not working as expected. Can anyone provide guidance on how to achieve ...

What is causing this JavaScript alert to malfunction?

My current project involves working with ASP.NET and in the view, I have a code snippet that is causing some issues. When I attempt to use Sweet Alert outside of the function, it works perfectly fine. Similarly, if I switch out Sweet Alert for a regular al ...

Preventing the occurrence of empty nested arrays within the state

I am currently working on a React Redux application where I am adding movies and including images as a nested array within my object. Here is what my state looks like after adding an item: { movies: [ { "title": "asda", "director": "as ...

Updating Vue component with mismatched props

I am looking to optimize the Vue component where data is received in varying structures. Take for example Appointment.vue component: <template> <div> <div v-if="config.data.user.user_id"> {{ config.data.user.user_id ...

Images of varying sizes aligned at the bottom of cards, organized in rows with pure CSS styling

I am working on a layout that involves a container with "cards": images positioned above with related text below. Here's an example: <div class = "cards"> <div class = "card"> <div class = "image-wrap"> <img src= "im ...

Adjust the border color of Material UI's DatePicker

Hello everyone, I am currently working with a DatePicker component from Material UI. My main goal is to change the border color of this component. I have attempted various methods such as modifying classes, adjusting the theme's primary color, and uti ...

How can you use CSS animations to animate two images in a way that hides one while showing the other?

click here for the image link visit this webpage for the link I need I am looking to add an animated section to my website. The inspiration comes from the webpage linked above, where images slide down one after another in a seamless manner. I attempted t ...

Having trouble with the copy to clipboard function of Prism JS on my NextJs application

I am facing an issue while trying to integrate a copy to clipboard plugin from prismjs into my next.js app. I have searched for documentation on this but couldn't find any relevant information. Despite going through various websites and implementing t ...

Challenge with the positioning of HTML output layout

Is there a way to center the output/result of the HTML code below both horizontally and vertically on the web page using CSS? I have tried the following code but it only centers the output horizontally. Vertical alignment is not working properly. Can someo ...

MySQL field being updated upon page unload

I'm currently developing a Content Management System (CMS) that allows users to access and organize records within a MySQL database. One of the features I have implemented is a user login system for the CMS. As part of this project, I am working on in ...

Seeking to have text spill over into a different container

Novice in CSS seeks advice. I've nailed the layout of the home page for my website with two divs</p> <p><div> <div> <pre class="snippet-code-css lang-css"><code>#desktop-navbar { text-transform: uppercase; ...

Serialize a series of select boxes to optimize for AJAX POST requests

To better explain my issue, let's consider a simple example: Imagine I have a form that collects information about a user: <form action="#" method="post" id="myform"> <input type="text" name="fname" /> <input type="text" name= ...