Home > other >  Set Input Value to jQuery Variable
Set Input Value to jQuery Variable

Time:08-10

I'm trying to set an input field to the value of a jQuery variable but it's refusing to cooperate. Can someone please tell me what I'm doing wrong? Essentially, I'm trying to get equipment failure counts to display in an input textbox so I can write the value back to a table. Each failure counts as 1 and is added to a total (truckFailsTot variable) when users click a Y/N radio button next to each type of failure. The example below just includes one failure for brevity.

HTML

div >
    <div ><label  for="TruckFailCount">TruckFailCount</label> 
<input  ${disabled( 'TruckFailCount')} id="TruckFailCount" maxlength="13" name="TruckFailCount" title="truckFailCount" type="text" value="${row.truckFailCount?html}" /> 
    </div>
</div>

jQuery - I've commented out my failed attemps below. It seems like such a simple thing, yet nothing is working.

    jQuery('body').on("change",'#TruckAirCompressor',function(){
        
         if (this.value == "Fail") {
             fail_TruckAirCompressor = 1;
    
         } 
         else if (this.value == "Pass") { 
             fail_TruckAirCompressor = 0;
             
         }
         else { 
    
         }
        
        
        truckFailsTot = fail_TruckAirCompressor   fail_TruckAirLines   fail_TruckBattery 
              fail_TruckBeltsHoses   fail_TruckBody   fail_TruckBrakesAccessories   fail_TruckBrakesParking
              fail_TruckBrakesService   fail_TruckClutch   fail_TruckCouplingDevicesHitch 
              fail_TruckDefrosterHeater   fail_TruckDriveLine   fail_TruckExhaustMuffler
              fail_TruckFluidLevels   fail_TruckFuelTanks   fail_TruckHorn   fail_TruckLights
              fail_TruckMirrors   fail_TruckSafetyEquipment   fail_TruckSteeringMechanism
              fail_TruckTiresRimsWheels   fail_TruckWindows   fail_TruckWindshieldWipers;


        //  var t = truckFailsTot;
        //  $('#TruckFailCount').value(t);
        //  document.getElementById("#TruckFailCount").innerHTML=t;

        //   var val1 = truckFailsTot;
        //  $('#TruckFailCount').val(val1);

        //   var fooBar = 1;
        //  $('#TruckFailCount').val(fooBar);

    }); 

Thanks in advance!

CodePudding user response:

I stumbled upon the correct syntax needed to assign a jQuery variable to an input text field and wanted to update my question in case someone else runs across a similar issue. All the jQuery documentation I'd come across recommended setting the value of the input field using .val(). Doing so didn't work for me, but the below code did.

var truckFailsTot;
truckFailsTot = fail_TruckAirCompressor   fail_TruckAirLines   fail_TruckBattery;
document.getElementById("TruckFailCount").value = truckFailsTot;
  • Related