0
I’m taking a data that is in an attribute of my select-option (date-observation), so far so good, however when I try to pass this value to my textarea the value is not printed, just on the console, anyone knows explain to me why that? Follows the code:
<select name="modelo" id="modelo" class="form-control">
<option value="" selected disabled>Selecione</option>
@foreach($modelos as $modelo)
<option value="{{ $modelo->id }}" data-observacao="{{ $modelo->corpo}}"> {{ $modelo->titulo }}
</option>
@endforeach
</select>
<div class="col-md-12 col-sm-12 mt-3" id="div-observacao">
<label for="">Observação</label>
<textarea class="form-control" name="observacao" id="observacao" rows="3"></textarea>
</div>
function removeTag(dataObservacao){
dataObservacao = dataObservacao.replace("<p>","");
dataObservacao = dataObservacao.replace("</p>","");
return dataObservacao;
};
$("#modelo").on("change", function (event) {
let dataObservacao = $("#modelo").find("option:selected").data("observacao");
dataObservacao = removeTag(dataObservacao);
console.log(dataObservacao);
console.log($("textarea[name='observacao']").val(dataObservacao));
$("textarea[name='observacao']").val(dataObservacao);
});
Here it worked normal. Now, since the textarea has an id, you can use it in the selector:
$("#observacao")
instead of$("textarea[name='observacao']")
.– Sam
Remembering that the script should be at the end of
body
... See this Fiddle how it works properly.– Sam
I agree with Sam. If in the console you are printing the value and not in the field. He is not finding the textarea object[name=note]
– Glenys Mitchell