0

I have the following hmtl page, is it possible to add a class on the div(line 3) based on the selection of the "Transaction Type". I Need to add a class "has-error" on line 3 of the below html ("div class="tab-pane active has-error" id="tab-first") if i am selecting transaction type "Expense" from the select option and I need to replace class to "has-success" if i am selecting "Income" from the select option.

<html>
    <body>
        <div class="tab-pane active" id="tab-first">

            <div class="form-group">
                <label class="col-md-2 col-xs-12 control-label">Transaction Type</label>
                <div class="col-md-3 col-xs-12">
                    <select id="wlt_txn_type" name="wlt_txn_type" class="form-control" required>
                        <option value="" disabled selected>Choose option</option>
                        <option value="Income">Income</option>
                        <option value="Expense">Expense</option>
                    </select>
                    <span class="help-block">Required</span>
                </div>
                <label class="col-md-2 col-xs-12 control-label">Transaction Date</label>
                <div class="col-md-3 col-xs-12">                                                                                                                                                        
                    <input id="wlt_txn_date" Name="wlt_txn_date"type="date" class="form-control" required/>
                    <span class="help-block">Required,No Future Date Allowed</span>
                </div>
            </div>

            <div class="form-group">
                <label class="col-md-2 col-xs-12 control-label">Select Wallet</label>
                <div class="col-md-3 col-xs-12">                                                                                                                                                        
                    <select id ="wlt_pcat" name="wlt_pcat" class="form-control" required>
                        <option value="" disabled selected>Choose Wallet</option>
                    </select>
                    <span class="help-block">Required</span>
                </div>
                <label class="col-md-2 col-xs-12 control-label">OD Limit</label>
                <div class="col-md-3 col-xs-12">                                                                                                                                                        
                    <input value="" class="form-control" readonly/>

                </div>
            </div>
        </div>  
    </body>
</html>

Thanks in Advance

4 Answers 4

2

It is Absolutely possible; Add a change event to the select and then check what the val() is of that select box, based on that add or remove class:

$(document).ready(function() {
  var tempvar = "";
  $("#wlt_txn_type").on("change", function() {
    tempvar = $("#wlt_txn_type").val();
    if (tempvar == "Income") {
      $("#tab-first").addClass('has-success').removeClass('has-error');
    } else
    if (tempvar == "Expense") {
      $("#tab-first").addClass('has-error').removeClass('has-success');
    }
  })
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="tab-pane active" id="tab-first">

  <div class="form-group">
    <label class="col-md-2 col-xs-12 control-label">Transaction Type</label>
    <div class="col-md-3 col-xs-12">
      <select id="wlt_txn_type" name="wlt_txn_type" class="form-control" required>
        <option value="" disabled selected>Choose option</option>
        <option value="Income">Income</option>
        <option value="Expense">Expense</option>
      </select>
      <span class="help-block">Required</span>
    </div>
    <label class="col-md-2 col-xs-12 control-label">Transaction Date</label>
    <div class="col-md-3 col-xs-12">
      <input id="wlt_txn_date" Name="wlt_txn_date" type="date" class="form-control" required/>
      <span class="help-block">Required,No Future Date Allowed</span>
    </div>
  </div>

  <div class="form-group">
    <label class="col-md-2 col-xs-12 control-label">Select Wallet</label>
    <div class="col-md-3 col-xs-12">
      <select id="wlt_pcat" name="wlt_pcat" class="form-control" required>
        <option value="" disabled selected>Choose Wallet</option>
      </select>
      <span class="help-block">Required</span>
    </div>
    <label class="col-md-2 col-xs-12 control-label">OD Limit</label>
    <div class="col-md-3 col-xs-12">
      <input value="" class="form-control" readonly/>

    </div>
  </div>
</div>

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

Comments

0

This can be done using jQuery like so:

// When the value changes
$("#wlt_txn_type").change(function () {
  // Get the value
  transactionType = $("#wlt_txn_type").val();
  // Add classes according to that value
  if (transactionType == "Expense") {
    $("#tab-first").addClass('has-success').removeClass('has-error');
  } else if (transactionType == "Income") {
    $("#tab-first").removeClass('has-success').addClass('has-error');
  }
});

Comments

0

Very easily done with jQuery:

$(document).ready(function(){
    
    $( "#wlt_txn_type" ).change(function() {
        if($("#wlt_txn_type option:selected").val() == "Income"){
            $("#tab-first").removeClass("has-error");
            $("#tab-first").addClass("has-success");        
        }
        if($("#wlt_txn_type option:selected").val() == "Expense"){
            $("#tab-first").removeClass("has-success");
            $("#tab-first").addClass("has-error");
        }
    });   
    
});
.has-success {
    background-color: green;
}
.has-error {
    background-color: red;
}
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="tab-pane active" id="tab-first">
    <div class="form-group">
        <label class="col-md-2 col-xs-12 control-label">Transaction Type</label>
        <div class="col-md-3 col-xs-12">
            <select id="wlt_txn_type" name="wlt_txn_type" class="form-control" required>
                <option value="" disabled selected>Choose option</option>
                <option value="Income">Income</option>
                <option value="Expense">Expense</option>
            </select> <span class="help-block">Required</span>

        </div>
        <label class="col-md-2 col-xs-12 control-label">Transaction Date</label>
        <div class="col-md-3 col-xs-12">
            <input id="wlt_txn_date" Name="wlt_txn_date" type="date" class="form-control" required/> <span class="help-block">Required,No Future Date Allowed</span>

        </div>
    </div>
    <div class="form-group">
        <label class="col-md-2 col-xs-12 control-label">Select Wallet</label>
        <div class="col-md-3 col-xs-12">
            <select id="wlt_pcat" name="wlt_pcat" class="form-control" required>
                <option value="" disabled selected>Choose Wallet</option>
            </select> <span class="help-block">Required</span>

        </div>
        <label class="col-md-2 col-xs-12 control-label">OD Limit</label>
        <div class="col-md-3 col-xs-12">
            <input value="" class="form-control" readonly />
        </div>
    </div>
</div>

Comments

0

If you are using twitter-bootstrap you need to add the has-success or has-error class to the form-group class.

To implement that, you need to use jquery.

JS:

$(function() {

    $('#wlt_txn_type').on('change', function() {
        if ($(this).val() == 'Expense') {
            $('#tab-first .form-group').addClass('has-error');
            $('#tab-first .form-group').removeClass('has-success');
        } else {
            $('#tab-first .form-group').addClass('has-success');
            $('#tab-first .form-group').removeClass('has-error');
        }
    });

});

Here's the JsFiddle link.


Hope it helps.

Comments

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.