0

I have multiple input fields where the user can select a date time, the only difference between these fields is the data-department attribute.

                <div class="col-sm-10">
                    <input type="datetime-local" class="StartTime" data-department="2" name="bdaytime">
                </div>

                <div class="col-sm-10">
                    <input type="datetime-local" class="StartTime" data-department="3" name="bdaytime">
                </div>

I want to be able to clear the value in an input field based on the data-department attribute.

How can I clear the value of an input field based on the class and data-department?

So for example if I want to be able to clear just a single input field where class equals StartTime and data-department equals 2?

I tried using the below code however it didn't work.

    $('input:datetime-local').find('data-department' = 2).val('');    
TidySB
  • 35
  • 7
  • 1
    Possible duplicate of [jQuery how to find an element based on a data-attribute value?](https://stackoverflow.com/questions/4191386/jquery-how-to-find-an-element-based-on-a-data-attribute-value) – CBroe Aug 29 '17 at 08:06
  • $('input.StartTime[data-department="2"]') – Thijs Aug 29 '17 at 08:06

3 Answers3

0

For clearing department 2

$('input[data-department="2"]').val(''); 

Here you go with a jsfiddle https://jsfiddle.net/y75tgzkb/

$('button').click(function(){
  $('input[data-department="2"]').val('');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="col-sm-10">
  <input type="datetime-local" class="StartTime" data-department="2" name="bdaytime">
</div>

<div class="col-sm-10">
  <input type="datetime-local" class="StartTime" data-department="3" name="bdaytime">
</div>

<button>
  Clear
</button>

In the code I'm only clearing department 2.

Hope this will help you.

Shiladitya
  • 12,003
  • 15
  • 25
  • 38
0

This selector should work:

$('input[type="datetime-local"][data-department="2"]').val('');
Abenitsi
  • 1
  • 1
0

To clear single input field check this fiddle: https://jsfiddle.net/y75tgzkb/2/

To clear multiple input fields,give any commen data-department to your code like data-department="11",data-department="12". Here1` is common then you can proceed like this

$( "input[data-department*='1']" ).val('');

working fiddle:https://jsfiddle.net/y75tgzkb/1/

krish
  • 1,077
  • 6
  • 24
  • 49