0

On a form, I have the following required drop down box:

<select id="SelectBox" name="SelectBox" required>
<option value="">Please Select ...</option>
<option value="val1">val1</option>
<option value="val2">val2</option>
<option value="val3">val3</option>
<option value="val4">val4</option>
</select>

I also have the following textbox:

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

I am trying to make the textbox required only if val4 is selected from the dropdown box using the required attribute (red border around textbox). I have done this with a radio button group, but need to do the same with this dropdown box.

Any help would be greatly appreciated. Thanks.

8
  • Are you using jquery? Commented Oct 9, 2016 at 16:02
  • Adding required attribute does not automatically require text input at element. Should text input also be required at #textbox element? Commented Oct 9, 2016 at 16:17
  • @guest271314 #textbox would be use in jQuery which isn't tagged. Commented Oct 9, 2016 at 16:18
  • @NewToJS "#textbox would be use in jQuery which isn't tagged." ? How does #textbox selector relate to jQuery? Commented Oct 9, 2016 at 16:19
  • @guest271314 well since the input ID is textbox and jQuery uses # to define ID's and . to define a class, using #textbox would relate to using a jQuery selector. How else would you use #textbox in pure javascript? Commented Oct 9, 2016 at 16:21

2 Answers 2

1

This should work to make text input attribute required and add some red border if you select the val4 option from the select box only.

 var select = document.getElementById("SelectBox");
 var textBoxElement = document.getElementById("textbox");

 select.onchange = function(){

    var selectedString = select.options[select.selectedIndex].value;

    if(selectedString == 'val4'){

       textBoxElement.required = true;
       textBoxElement.style.border="1px solid red";

    }else{

       textBoxElement.required = false;
       textBoxElement.style.border="";
    }   
}

See working fiddle : https://jsfiddle.net/nwk1tkb7/5/

Sign up to request clarification or add additional context in comments.

2 Comments

What if I choose val2 after selecting val4.
strange...I see that the jfiddle works, but when I apply to my page, it is not working. Could there be a reason? The page I am using is in a WordPress site, but I dont think that matters because other JS functions are working. Then, I also copied and pasted your html and JS in a regular html page and it still did not work. And I did add the opening and closing script tags.
0

One approach I'd suggest is the following:

// use of a named function, to be called via Elememt.addEventListener:
function elementRequiredIf(event) {

  // 'this' will be the <select> element in this example, however
  // the 'this' is automatically passed from Element.addEventListener
  // (as is the 'event' object, though we don't use that here):
  var changed = this,

  // here we find the conditionally-required element, using
  // the id of the element stored in the custom data-* attribute
  // of the <select> element, here we retrieve that attribute-
  // value from the dataset object, stored as a property of the
  // Element:
    requiredElement = document.getElementById(changed.dataset.required_elem_id),

  // as above, we use the same approach to retrieve the
  // value that makes the other element required:
    ifRequiredValueIs = changed.dataset.required_if_value;

  // here we set the required property of the element to
  // true (if the <select> value is equal to the required
  // value, or false if they are unequal:
  requiredElement.required = changed.value === ifRequiredValueIs;
}

// here we find the <select> element via its id,
// and bind an event-listener for the 'change' event,
// which, upon a change event being fired, calls the
// named function (note the deliberate, required, lack
// of parentheses following the function name):
document.getElementById('SelectBox').addEventListener('change', elementRequiredIf);

function elementRequiredIf() {
  var changed = this,
    requiredElement = document.getElementById(changed.dataset.required_elem_id),
    ifRequiredValueIs = changed.dataset.required_if_value;
  requiredElement.required = changed.value === ifRequiredValueIs;
}

document.getElementById('SelectBox').addEventListener('change', elementRequiredIf);
input:required {
  box-shadow: 0 0 0.5em #f00;
}
<form>
  <select id="SelectBox" name="SelectBox" data-required_elem_id="textbox" data-required_if_value="val4" required>
    <option value="">Please Select ...</option>
    <option value="val1">val1</option>
    <option value="val2">val2</option>
    <option value="val3">val3</option>
    <option value="val4">val4</option>
  </select>

  <input type="text" name="textbox" id="textbox">
  <button type="submit">Submit</button>
</form>

JS Fiddle demo.

References:

4 Comments

Setting required attribute does not automatically require text input at element. The <form> can still be submitted without any text value at the element; e.g., an empty string as value. OP has not clarified if actual text input at the element is required by user.
@guest271314: Setting required certainly prevents Chrome from submitting the form without an entry, though admittedly I've not tested cross-browser. Also it's worth noting that I'm setting the required property, rather than the attribute (which doesn't always reflect the updated property-value).
At jsfiddle, try selecting val4, focus at #textbox element, press space key, press Submit
Yes, it makes the element required, if the OP wants to know how to determine the presence of a value then that's a separate topic that is entirely absent from the question as asked. I'm happy to add the details, if required, but it is, as yet, unasked for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.