Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - Keyboard Enter to behave as a - click on a label button?

Tags:

jquery

php

I have some sort of a jquery chat at one specific PHP page. Of course, bellow that chat area there is an input field (jquery form), and after you click on a label id button, jQuery gets your words, username, stores data into MySql and reloads chat again. Funny lightweight and neat. Reason no1 why you must love jquery.

Anyway, most important to say, that "button" is not submit button - its a "label id" button.

Thats quite okay, but when you get hot into that chat, and you suddenly press enter, instead you click on a label button - PHP page just refreshes and does nothing with data what makes everyone crazy.

So, any hot tip how to send keyboard enter as a click on that label id button?


UPDATE:

<form id="chav_box_in" style="padding:0px;margin:0px;" action="" method="post">
      <div class="chav_level" valign="top"><input type="hidden" id="chav_in" name="chav_in" value="'.$categoryid.'" />
      ' . $lg['your_nick'] . ':&nbsp;&nbsp;';
    if($userinfo['loggedin'])
    {
        if($getnamen = $DB->query_first("SELECT userid, jur_name FROM " . TABLE_PREFIX . "users WHERE userid = '".$userinfo['userid']."'"))
        {
        echo '<input type="hidden" id="chav_username" name="chav_username" value="'.$getnamen['jur_name'].'" /><span class="chav_name">'.$getnamen['jur_name'].'</span>';
        }
        else
        {
    echo '<input type="hidden" id="chav_username" name="chav_username" value="'.$getnamen['userid'].'" /><span class="chav_name">Registered undefined ('.$getnamen['userid'].')</span>';
        }
    echo '<input type="hidden" id="chav_lag" name="chav_lag" value="1" />';
    }

    else // unergs
    {
        if($nemere) //cookieid
        {
        echo '<input type="hidden" id="chav_username" name="chav_username" value="'.$nemere.'" /><span class="chav_name">'.$nemere.'</span>';
        }
        else   //nov upisivac
        {
        echo '<input type="text" size="' . $inputsize . '" id="chav_username" name="chav_username" value="" />';
        }
    }

echo '&nbsp;&nbsp;' . $lg['occ_place'] . ':&nbsp;';

// ##########
    if($categoryid == 6)
    {
    echo '<input type="text" size="' . $inputsize . '" id="chav_sub" name="chav_sub" value="" /></div>';
    }
    else
    {
    echo '<input type="hidden" id="chav_sub" name="chav_sub" value="'.$url_places_name.'" /><span class="chav_name">'.$url_places_name.'</span></div>';
    }


    echo '<div class="chav_level">' . $lg['chav_input'] . '&nbsp;&nbsp;<input type="text" size="' . ($inputsize*5) . '" id="wm1" name="wm1" value="" />
     &nbsp;<label id="shareButton1" class="enter" style="display:inline;float:right;">' . $lg['publish'] . '</label></div>';


    echo '</form>

Now, when I enter something in that input field and press enter, page refreshes.. no mind for a mess im cleaning it out.. AND query

$(function () {
$('chav_box_in').keypress(function(e) { // Attach the form handler to the keypress event
    if (e.keyCode == 13) { // If the the enter key was pressed.
        $('#shareButton1').click(); // Trigger the button(elementId) click event.
        return e.preventDefault(); // Prevent the form submit.
    }
});
});
like image 798
Xfile Avatar asked Dec 05 '25 09:12

Xfile


1 Answers

$('form').keypress(function(e) { // Attach the form handler to the keypress event
    if (e.keyCode == 13) { // If the the enter key was pressed.
        $('#elementId').click(); // Trigger the button(elementId) click event.
        return e.preventDefault(); // Prevent the form submit.
    }
}​);​

Now you can get hot in the chat, and enter will work for you just like clicking the button... Which I think should work this way anyway.

like image 74
gdoron is supporting Monica Avatar answered Dec 07 '25 23:12

gdoron is supporting Monica



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!