How to read with Jquery and element readonly for django
Object1
see the image plx
there are some readonly field that i set up on django, but i want to read the values using Jquery but really idk how to do it, i want to do it cuz depending of the value that is set on field "tipo" i want a make some values visibles or not.
let tipo_persona_check = function(){
if ($('#id_tipo').val() == 'fisica'){
$('.field-empresa').hide();
$('label[for=id_persona').text('Suscriptor');
}else{
$('label[for=id_persona').text('Apoderado');
$('.field-empresa').show();
}
in this case im hidding or showing field empresa depending of the value of id_tipo, but when it is on readonly, this id not exist, there is just a span class called readonly (for all readonly) with the value, but idk how to get from it, i need to know if the value is "fisica" or another from the readonly field tipo, to do things.
any ideas?
I did not check whether the ID is really visible when this is the case
We assume that the ID is not seen.
The solution is that you can add an input in that section so that you can find the ID from it.
admin.py
@admin.register(SomeModel)
class SomeModelAdmin(admin.ModelAdmin):
def id_field(self, obj):
return mark_safe(f"<input type='hidden' id='ID' value='{obj.pk}'>")
readonly_fields = ["id_field"]
and in you js file:
id = document.getElementById('ID').value