Assign value to a specific input

Asked

Viewed 1,254 times

3

Good morning person,

I’m having trouble coming up with a code.

I have several inputs that are created dynamically. I need to take the ID of the specific element to change its value.

EX: I have the following fields: Days, Absences, Vacancy, Leave, Sick Leave and Work Accident. The Days field has a value of 30, the other fields may vary depending on the employee. The sum of all fields(Absences, Vacancy, License, Medical License and Work Accident) can not exceed 30, moreover, as soon as the user enters something in these fields he has to immediately update the field Days reducing its value.

Follow the code below for better understanding.

@foreach ($servidores as $i => $servidor)
<form id="formid{{$i++}}" role="form" method="get" action="/mapa_servidor/editMapaServidor/{{$servidor->id}}">
    <tr data-index="0" id="formulario{{$i}}" class="input-sm" {{$servidor->st_lancado == 'X'? $cor = "#38A8A2": $cor = "FFFFFF"}} style="background-color: {{$cor}}">
        <td class="text-left">
            <input type="text" class="nm_servidor" id="nm_servidor{{$i}}" name="nm_servidor[]" value="{{$servidor->nm_servidor}}" readonly>
            <input type="hidden" id="dt_referencia" name="dt_referencia" value="{{$servidor->dt_referencia}}" readonly>
            <input type="hidden" name="_token" value="{{csrf_token()}}" />
            <br>
        </td>
        <td class="text-left">
            <input type="text" class="nr_matricula" id="nr_matricula{{$i}}" name="nr_matricula[]" value="{{$servidor->nr_matricula}}" readonly>
        </td>
        <td class="text-left">
            <input type="text" class="cd_departamento" id="cd_departamento{{$i}}" name="cd_departamento[]" value="{{$servidor->cd_departamento}}" readonly>
        </td>
        <td class="text-left" style="">
            <input type="text" class="total nr_dias" id="nr_dias{{$i}}" name="nr_dias[]" value="{{rtrim($servidor->nr_dias)}}" readonly>
        </td>
        <td class="text-left" style="">
            <input type="text" class="calc nr_faltas" id="nr_faltas{{$i}}" name="nr_faltas[]" pattern="[0-9]+$" value="{{rtrim($servidor->nr_faltas)}}" {{$servidor->st_lancado == 'X'? 'readonly': '' }}>
        </td>
        <td class="text-left" style="">
            <input type="text" class="calc nr_vacancia" id="nr_vacancia{{$i}}" name="nr_vacancia[]" pattern="[0-9]+$" value="{{rtrim($servidor->nr_vacancia)}}" {{$servidor->st_lancado == 'X'? 'readonly': '' }}>
        </td>
        <td class="text-left" style="">
            <input type="text" class="calc nr_licenca" id="nr_licenca{{$i}}" name="nr_licenca[]" pattern="[0-9]+$" value="{{rtrim($servidor->nr_licenca)}}" {{$servidor->st_lancado == 'X'? 'readonly': '' }}>
        </td>
        <td class="text-left" style="">
            <input type="text" class="calc nr_licenca_medica" id="nr_licenca_medica{{$i}}" name="nr_licenca_medica[]" pattern="[0-9]+$" value="{{rtrim($servidor->nr_licenca_medica)}}" {{$servidor->st_lancado == 'X'? 'readonly': '' }}>
        </td>
        <td class="text-left" style="">
            <input type="text" class="calc nr_acidente_trabalho" id="nr_acidente_trabalho{{$i}}" name="nr_acidente_trabalho[]" pattern="[0-9]+$" value="{{rtrim($servidor->nr_acidente_trabalho)}}" {{$servidor->st_lancado == 'X'? 'readonly': '' }}>
        </td>
        <td class="text-left" style="">
            <input type="text" class="nr_adicional_noturno" id="nr_adicional_noturno{{$i}}" name="nr_adicional_noturno[]" value="{{rtrim($servidor->nr_adicional_noturno)}}" {{$servidor->st_lancado == 'X'? 'readonly': '' }}>
        </td>
        <td class="text-left" style="">
            <input type="text" class="nr_he_norm_diurno" id="nr_he_norm_diurno{{$i}}" name="nr_he_norm_diurno[]" value="{{rtrim($servidor->nr_he_norm_diurno)}}" {{$servidor->st_lancado == 'X'? 'readonly': '' }}>
        </td>
        <td class="text-left" style="">
            <input type="text" class="nr_he_norm_noturno" id="nr_he_norm_noturno{{$i}}" name="nr_he_norm_noturno[]" value="{{rtrim($servidor->nr_he_norm_noturno)}}" {{$servidor->st_lancado == 'X'? 'readonly': '' }}>
        </td>
        <td class="text-left" style="">
            <input type="text" class="nr_he_dom_diurno" id="nr_he_dom_diurno{{$i}}" name="nr_he_dom_diurno[]" value="{{rtrim($servidor->nr_he_dom_diurno)}}" {{$servidor->st_lancado == 'X'? 'readonly': '' }}>
        </td>
        <td class="text-left" style="">
            <input type="text" class="nr_he_dom_noturno" id="nr_he_dom_noturno{{$i}}" name="nr_he_dom_noturno[]" value="{{rtrim($servidor->nr_he_dom_noturno)}}" {{$servidor->st_lancado == 'X'? 'readonly': '' }}>
        </td>
        <td class="text-left" style="">
            <input type="checkbox" class="st_grat_reuniao" id="st_grat_reuniao{{$i}}" name="st_grat_reuniao[]" {{($servidor->st_grat_reuniao) == 'X'? 'checked': '' }} {{$servidor->st_lancado == 'X'? 'readonly': '' }}>
        </td>
        <td class="text-left" style="">
            <input type="text" class="ds_observacao" id="ds_observacao{{$i}}" name="ds_observacao[]" value="{{rtrim($servidor->ds_observacao)}}" {{$servidor->st_lancado == 'X'? 'readonly': '' }}>
        </td>
        <td class="text-left" style="">
            <input type="checkbox" class="st_lancado" id="st_lancado{{$i}}" name="st_lancado[]" {{$servidor->st_lancado == 'X'? 'checked': '' }} required>
        </td>
        <td class="text-left" style="">
            <button type="submit" class="btn btn-default btn-xs">Salvar</button>
        </td>
    </tr>
</form>

@endforeach

The way I did, it is modifying all the inputs of name nr_dias. Instead of modifying only the line I need. I know I’m doing it wrong, but I can’t think of a solution.

$(document).on("keyup", ".calc", function() {
		var sum = 0;

		$(".calc").each(function(){
			sum += +$(this).val();
		});
		if(sum > 30){
			$(".total").val(30);
			$(".calc").val('');
		}else{

			$(".total").val(30-sum);
		}
	});

  • I think the title of the question does not match the scope of the question...

  • The problem has to be in the question and you know that the problem is not in how to get the element id because you already know how to do it.

  • Another mistake is that you put in the title: javascript, but put tag jquery, leaves confused about who will answer... Should I answer in javascript or jquery?

  • 1

    I am saying this because it will help you a lot to get something here. I also went through this, don’t be discouraged!

  • Lollipop no problem. I will take more care next time. I changed the title of the question and removed the javascript tag.

  • I see no problem in leaving the tag javascript provided that it also jquery. You can solve only with the logic of javascript with nothing to jquery, the problem could be a if for example ;-)

Show 1 more comment

2 answers

3


You don’t need the ID, but you only need to use inputs from the same line.

In your code, where you reference the elements, here for example:

$(".total").val(30-sum);

you need to pick up the reference only from the same line, ie the same TR, thus:

$(this).closest('tr').find(".total").val(30-sum);

The this take the reference of input which is being amended, the closest('tr') take the reference of TR and finally used the find(".total") to search the controls only nested with the TR.

This should be done for all references. See the simplified example:

$(document).on("keyup", ".calc", function() {
	var sum = 0;

	$(this).closest('tr').find(".calc").each(function(){
	    sum += +$(this).val();
	});
	if(sum > 30){
	    $(this).closest('tr').find(".total").val(30);
	    $(this).closest('tr').find(".calc").val('');
	}else{
	    $(this).closest('tr').find(".total").val(30-sum);
	}
});
input {
  width: 50px
  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<table>
    <tr data-index="0" >
        <td class="text-left" style="">
            <input type="text" class="total nr_dias"  name="nr_dias[]" value="30" readonly>
        </td>
        <td class="text-left" style="">
            <input type="text" class="calc nr_faltas"  name="nr_faltas[]">
        </td>
        <td class="text-left" style="">
            <input type="text" class="calc nr_vacancia"  name="nr_vacancia[]">
        </td>
        <td class="text-left" style="">
            <input type="text" class="calc nr_licenca"  name="nr_licenca[]" >
        </td>
        <td class="text-left" style="">
            <input type="text" class="calc nr_licenca_medica"  name="nr_licenca_medica[]">
        </td>
        <td class="text-left" style="">
            <input type="text" class="calc nr_acidente_trabalho"  name="nr_acidente_trabalho[]" >
        </td>
    </tr>

    <tr data-index="1" >
        <td class="text-left" style="">
            <input type="text" class="total nr_dias"  name="nr_dias[]" value="30" readonly>
        </td>
        <td class="text-left" style="">
            <input type="text" class="calc nr_faltas"  name="nr_faltas[]">
        </td>
        <td class="text-left" style="">
            <input type="text" class="calc nr_vacancia"  name="nr_vacancia[]">
        </td>
        <td class="text-left" style="">
            <input type="text" class="calc nr_licenca"  name="nr_licenca[]" >
        </td>
        <td class="text-left" style="">
            <input type="text" class="calc nr_licenca_medica"  name="nr_licenca_medica[]">
        </td>
        <td class="text-left" style="">
            <input type="text" class="calc nr_acidente_trabalho"  name="nr_acidente_trabalho[]" >
        </td>
    </tr>


    <tr data-index="2" >
        <td class="text-left" style="">
            <input type="text" class="total nr_dias"  name="nr_dias[]" value="30" readonly>
        </td>
        <td class="text-left" style="">
            <input type="text" class="calc nr_faltas"  name="nr_faltas[]">
        </td>
        <td class="text-left" style="">
            <input type="text" class="calc nr_vacancia"  name="nr_vacancia[]">
        </td>
        <td class="text-left" style="">
            <input type="text" class="calc nr_licenca"  name="nr_licenca[]" >
        </td>
        <td class="text-left" style="">
            <input type="text" class="calc nr_licenca_medica"  name="nr_licenca_medica[]">
        </td>
        <td class="text-left" style="">
            <input type="text" class="calc nr_acidente_trabalho"  name="nr_acidente_trabalho[]" >
        </td>
    </tr>

</table>

  • 1

    Ricardo Thank you so much for your help. Your code solved my problem. Thank you for your attention. Have a good day.

-2

$(document).on("keyup", ".calc", function() {
	var sum = 0;

	$(this).closest('tr').find(".calc").each(function(){
	    sum += +$(this).val();
	});
	if(sum > 30){
	    $(this).closest('tr').find(".total").val(30);
	    $(this).closest('tr').find(".calc").val('');
	}else{
	    $(this).closest('tr').find(".total").val(30-sum);
	}
});
input {
  width: 50px
  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<table>
    <tr data-index="0" >
        <td class="text-left" style="">
            <input type="text" class="total nr_dias"  name="nr_dias[]" value="30" readonly>
        </td>
        <td class="text-left" style="">
            <input type="text" class="calc nr_faltas"  name="nr_faltas[]">
        </td>
        <td class="text-left" style="">
            <input type="text" class="calc nr_vacancia"  name="nr_vacancia[]">
        </td>
        <td class="text-left" style="">
            <input type="text" class="calc nr_licenca"  name="nr_licenca[]" >
        </td>
        <td class="text-left" style="">
            <input type="text" class="calc nr_licenca_medica"  name="nr_licenca_medica[]">
        </td>
        <td class="text-left" style="">
            <input type="text" class="calc nr_acidente_trabalho"  name="nr_acidente_trabalho[]" >
        </td>
    </tr>

    <tr data-index="1" >
        <td class="text-left" style="">
            <input type="text" class="total nr_dias"  name="nr_dias[]" value="30" readonly>
        </td>
        <td class="text-left" style="">
            <input type="text" class="calc nr_faltas"  name="nr_faltas[]">
        </td>
        <td class="text-left" style="">
            <input type="text" class="calc nr_vacancia"  name="nr_vacancia[]">
        </td>
        <td class="text-left" style="">
            <input type="text" class="calc nr_licenca"  name="nr_licenca[]" >
        </td>
        <td class="text-left" style="">
            <input type="text" class="calc nr_licenca_medica"  name="nr_licenca_medica[]">
        </td>
        <td class="text-left" style="">
            <input type="text" class="calc nr_acidente_trabalho"  name="nr_acidente_trabalho[]" >
        </td>
    </tr>


    <tr data-index="2" >
        <td class="text-left" style="">
            <input type="text" class="total nr_dias"  name="nr_dias[]" value="30" readonly>
        </td>
        <td class="text-left" style="">
            <input type="text" class="calc nr_faltas"  name="nr_faltas[]">
        </td>
        <td class="text-left" style="">
            <input type="text" class="calc nr_vacancia"  name="nr_vacancia[]">
        </td>
        <td class="text-left" style="">
            <input type="text" class="calc nr_licenca"  name="nr_licenca[]" >
        </td>
        <td class="text-left" style="">
            <input type="text" class="calc nr_licenca_medica"  name="nr_licenca_medica[]">
        </td>
        <td class="text-left" style="">
            <input type="text" class="calc nr_acidente_trabalho"  name="nr_acidente_trabalho[]" >
        </td>
    </tr>

</table>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.