Muhammad Russel Muhammad Russel - 3 months ago 16
HTML Question

javascript autoclick with condition

I want to make a form where if someone type the word

ok
in the input field, the button will be auto clicked. How can this be done?

<input type="text" id="text"><br><br>

<a href="#" id="autoclick">
<input type="button" value="type ok">
</a>



Answer

Try this:

HTML:

<input type="text" id="text"><br><br>
<input type="button" id="button" value="type ok">

jQuery:

$('#text').on('keyup', function(){
    if($(this).val() == 'ok'){
        $('#button').trigger('click');
    }
})
// this block is for test
$('#button').on('click', function() {
    alert('clicked');
});

Working example: https://jsfiddle.net/fnoL4j7m/1

Comments