show and hide items

0

I would like you to help me, I need to know if it is possible to do this or I am just wasting my time. I need to hide / show certain fields that are a combobox, I know that they can be hidden or displayed when selecting a checkbox or a combobox, but I need to know if it can be done through an inputs name, actually if it can also be done, but when I want to reload the page and modify the information the input field that I bring with an X value does not do anything. When I start to write my form I have an input age and according to age I see two hidden fields, until there all right, but when I modify the same information does not do anything if the age entered previously was visible. I have the following code that I am working with.

$(document).ready(function (){
$("input[name='edad']").change(function() {
    if($(this).val()>4){
        $("#est").fadeIn();
        $("#act").fadeIn();
    }else{
        $("#est").fadeOut();
        $("#act").fadeOut();
    }
});
    
asked by Juanjo Medina 27.11.2017 в 18:58
source

2 answers

1

Because you only do that when you make the change, you have to put it in the document ready too

$(document).ready(function (){
if($("input[name='edad']").val()>4){
        $("#est").fadeIn();
        $("#act").fadeIn();
    }else{
        $("#est").fadeOut();
        $("#act").fadeOut();
    }
$("input[name='edad']").change(function() {
    if($(this).val()>4){
        $("#est").fadeIn();
        $("#act").fadeIn();
    }else{
        $("#est").fadeOut();
        $("#act").fadeOut();
    }
});

If you want to make it cleaner create that in a function and send it to call in both cases.

    
answered by 27.11.2017 / 19:10
source
0

To your input, add a class that you want to be .inputChange and add this to the end of your JS after the values have already been loaded in your input:

$(".inputChange").change();

If they are going to be more than 1 input you can add them to all the ones you need the same class and add this at the end instead of the previous one.

$(".inputChange").each(function(){
 $(this).change();
});

This will cause you to execute the change() function of all the elements you need

I hope it serves you.

    
answered by 27.11.2017 в 19:11