Change "on" to "click" in Javascript function

Asked

Viewed 51 times

2

I want when I click on the radios to call the function, currently this way.

$("input[name='txtCategoria']").on('blur', function(){
    var txtCategoria = $(this).val();
    $.get('buscar_tipos.php?txtCategoria=' + txtCategoria,function(data){
        $('#tipos').html(data);
    });
});

However only when I click on the radio and click on another part of the page that appears the query, I wanted that when it clicks it is already executed, I tried so.

$("input[name='txtCategoria']").on("click", function(){
    var txtCategoria = $(this).val();
    $.get('buscar_tipos.php?txtCategoria=' + txtCategoria,function(data){
        $('#tipos').html(data);
    });
});

But it didn’t work, someone could help me?

1 answer

3


blur that is to say "when the element loses focus". This is what happens when you click outside of this element, and the function runs. Because it "lost focus".

The most semantic would be to use the change, which is when the input change value. So, even for those who navigate and change input with the keyboard, the logic will work.

$("input[name='txtCategoria']").on("change", function() {
  $.get('buscar_tipos.php?txtCategoria=' + this.value, function(data) {
    $('#tipos').html(data);
  });
});
  • 1

    Thanks a lot friend, I’m starting now on Javascript I’m catching a lot kkk.

  • 1

    @Evertonfigueiredo glad I could help. Note that I used this.value. This code does the same, and faster/simpler than $(this).value();

  • 1

    @Evertonfigueiredo Notes that your example with click should work as well (for clicks). You may need to delegate the event. If you have problems says.

  • 1

    I only made the change from Blur to change and it worked perfectly.

Browser other questions tagged

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