Delete Label text with jquery

Asked

Viewed 838 times

1

I need to delete the text contained within the tag label ("Justification") with jquery, follow the code:

function add_text() {
  if ($("#tipo").val() == "Falta") {
    $(".b").remove();
    $("#just").remove();
    $("#justificativa").remove();
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="tipo" name="tipo" onchange="add_text()"><option>Falta</option><option>Falta Justificada</option><option>Fardamento Incompleto</option><option selected="">Entrada/Saída Autorizada</option><option>Indisciplína/Outros</option></select><br>
<label>Justificativa: </label><br><textarea required placeholder='Digite a justificativa do aluno...' title='Seja objetivo(a).' rows='5' name='obs' cols='50' id='justificativa'></textarea>

  • 1

    She may have ID ?

  • Yes @teliz can yes.

1 answer

2


Add an ID to the label

<label id="label-justificativa">Justificativa: </label>

Add to your js

$("#label-justificativa").remove();

--

I think it might improve your code:

    function add_text(){     
    	if($("#tipo").val()=="Falta"){
             $(".b").remove();
             $("#just").remove();
             $("#opcoes").hide();
        }
        else{
        	$("#opcoes").show()
        }
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="tipo" name="tipo" onchange="add_text()">
       <option>Falta</option>
       <option>Falta Justificada</option>
       <option>Fardamento Incompleto</option>
       <option selected="">Entrada/Saída Autorizada</option>
       <option>Indisciplína/Outros</option>
    </select>
    <div id="opcoes">
       <label>Justificativa: </label><br>
       <textarea required placeholder='Digite a justificativa do aluno...'
       title='Seja objetivo(a).' rows='5' name = 'obs' cols='50' 
       id='justificativa'></textarea>
    </div>

I made a modification that it is only hidden when selected value is "Missing"

So you remove an entire block instead of searching for the tags you want to remove, I see it as a simpler solution

you can also use the Hide() to hide the html, so if you want to display it again use the show().

Browser other questions tagged

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