Click event is not working

Asked

Viewed 62 times

1

I’m trying to create a dynamic object for a click event.

Every time a page is loaded, the data that is saved in the database will be filled in at the front.

The click event has the same rule as the event that checks if the element is checked as soon as the page is loaded.

var teste = {
    testando: function(element){
        $(element).on("click",function(){    
            var valor = $(element + ":checked").val();
            this.uai(valor);    
            alert("teste");
        });

        if(($(element)).is(":checked")) {
            var valor = $(element + ":checked").val();
            this.uai(valor);
        }
    },
    uai : function(valor){
        //faz algo com o valor, como por exemplo: deixar uma div invisível ou não            
    }
};


$(document).ready(function(){
      $(".radio1").get("2").checked = true;
    teste.testando("[name='radio1']");
});

jsfiddle: https://jsfiddle.net/9rmu2jpw/2

1 answer

0

See if I got it right:

var teste = {};

teste.uai = function(valor){
         alert(valor);            
    };

teste.testando = function(element){    
        if(($(element)).is(":checked")) {
            var valor = $(element + ":checked").val();
            this.uai(valor);
        }
    };

$(document).ready(function(){
      $(".radio1").get("2").checked = true;
    teste.testando(".radio1:checked");
    $(".radio1").on("click",function(){                 
        var valor = $(".radio1:checked").val();
        teste.uai(valor);    
                alert("teste");
    });
});

Fiddle: https://jsfiddle.net/9rmu2jpw/7/

  • is that, but I wanted to do the event by clicking inside the object, would save an encoding save (ie, would just call the "test.testing" by passing the element...)

  • The way you did the click event will only fire when the first element that was loaded on the page as checkado is clicked. That’s what you want ?

  • i wanted this event to always be triggered when the element was clicked, but how do I work using a click event inside the object?

  • When you use inside the object, you are always using the same selector, which is "[name='radio1']", which you call 1 time when loading the document (within Document.ready). To always grab the element that is clicked you can not put this event click inside the object.

Browser other questions tagged

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