Govind Malviya
Govind Malviya

Reputation: 13763

Keypress event not working

I am using this code but keypress event not working

 <script type="text/javascript">
    $(document).ready(function() {
        $('#txt_tempusername').keypress(function() {

            var href = $('#providerurl').val();
            href = href.toString().replace("{username}", $('#txt_tempusername').val());
            $('#btn_idgo').attr('href', href);

        });
    });

</script>

and this is my HTML

<div class="Input_Div">
 <input type="text" id="txt_tempusername" />
 <a class='example1demo' id="btn_idgo">Go&lt;/a>
 <input type="hidden" id="providerurl" />
</div>

Upvotes: 1

Views: 4796

Answers (3)

user28919203
user28919203

Reputation: 1

<script src="https://code.jquery.com/jquery-3.6.0.js"></script>

$(document).on('keypress', function(e){
    if(e.which == 13){
        e.preventDefault;
        console.log('323');
    }
});

Upvotes: 0

ezmilhouse
ezmilhouse

Reputation: 9131

Working sample here: http://jsfiddle.net/ezmilhouse/6zfw8/2/

Guess the events worked fine but your 'href' treatment didn't work because the hidden fields value was not defined.

Fixed your code the way I think you wanted it to work:

your html:

<div class="Input_Div">
    <input type="text" id="txt_tempusername" />
    <a class='example1demo' id="btn_idgo">Go!</a>
    <input type="hidden" id="providerurl" value="http://provider-url-{username}.html" />
</div>

your js:

$(document).ready(function() {
    $('#txt_tempusername').keyup(function() {
        var href = $('#providerurl').val().replace("{username}", $(this).val());
        $('#btn_idgo').attr('href', href);
    });
});

Upvotes: 3

cweiske
cweiske

Reputation: 31146

Put an

`alert("foo");`

in the function and see if you get a message box. If you don't get one, the element with the ID txt_tempusername does not exist.

Upvotes: 1

Related Questions