1

I have two fields which appear as the data is already entered before:

enter image description here

On certain condition an if condition is called and these value needs to be cleared. I tried to pass blank string inside val() so that it becomes empty but it is not working, data is still displayed:

<div class="col-md-12 fromDates" style="margin-bottom: 10px; display: block;">
   <label for="fromDate">From Date</label>
   <input type="text" id="assessFrom" required="" name="assessFrom">
</div>
<div class="col-md-12 toDates" style="margin-bottom: 10px; display: block;">
   <label for="fromDate">To Date</label>
   <input type="text" id="assessTo" required="" name="assessTo">
</div>
if (assessmentType == "firm" || assessmentType == "product") {
  $('.fromDates').val('');
  $('.toDates').val('');
}  
3
  • 1
    codepen or jsfiddle link please ? Commented Nov 30, 2018 at 11:40
  • 1
    you can use a cleverer selector which will get the inputs inside your classes, like: $('.fromDates input') instead of $('.fromDates'). Commented Nov 30, 2018 at 11:41
  • Target the input field rather than your div container Commented Nov 30, 2018 at 12:22

4 Answers 4

4

.fromDates and .toDates are div elements which have no value.

Instead you need to clear the value of the input elements within them:

if (assessmentType == "firm" || assessmentType == "product") {
  $('.fromDates input, .toDates input').val('');
}  

Alternatively you could use the id on the inputs directly:

if (assessmentType == "firm" || assessmentType == "product") {
  $('#assessFrom, #assessTo').val('');
}
Sign up to request clarification or add additional context in comments.

Comments

1

when you have Id why you are using class to clear the data from input field,

$('#assessFrom').val('');
$('#assessTo').val('');

and if you still want to use javascript

document.getElementById('assessFrom').value = '';
document.getElementById('assessTo').value = '';

Comments

0

Use the selector as input elements

$('#assessFrom').val('');
$('#assessTo').val('');

Comments

-1

I hope the below code will solve the issue, you can change the if condition based on your condition.

function check(){
  if(true){
   var item = document.getElementById("assessFrom");
   item.value = "";
  }
}
<div class="col-md-12 fromDates" style="margin-bottom: 10px; display: block;">
   <label for="fromDate">From Date</label>
   <input type="text" id="assessFrom" required="" name="assessFrom" value="08-07-2018">
</div>
<div class="col-md-12 toDates" style="margin-bottom: 10px; display: block;">
   <label for="fromDate">To Date</label>
   <input type="text" id="assessTo" required="" name="assessTo" value="12-01-2018">
</div>

<button onclick="check()">Check</button>

1 Comment

Downvote isn't mine, but there's a couple of code smells here; the onclick attribute, the if (true)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.