Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable or disable form elements in jquery

Tags:

jquery

How do I properly disable and enable form elements using jquery. I need to disable the text form element when I click on the select. And vise versa.

<html>
    <head>
    <script src="jq.js"></script>
    <script>

    $(function(){

    $('#fromdate').click(function(){
        $('#yosh').attr('disabled','disabled');
        $('#fromdate').removeAttr('disabled');
    });

    $('#yosh').click(function(){
        $('#yosh').removeAttr('disabled');
        $('#fromdate').attr('disabled','disabled');
    });

    });
    </script>

    </head>
    <body>
    Sort by:
    <select name="yosh" id="yosh">
    <option value="daily">daily</option>
    <option value="weekly">yesterday</option>
    <option value="weekly">weekly</option>
    <option value="monthly">monthly</option>
    <option value="yearly">yearly</option>

    </select><br/>
    date range:<br/>

    From:<input type="text" value="" name="fromdate" id="fromdate"></input><br/>
    To:<input type="text" value="" name="todate" id="todate"></input><br/>


    Customer:<input type="text" value="" name="customer" id="customer"></input>

    </body>

    </html>
like image 610
Wern Ancheta Avatar asked Feb 11 '11 05:02

Wern Ancheta


2 Answers

Example of what i've written in the comment:

<span id="spnSel">
    <select name="yosh" id="yosh">
        <option value="daily">daily</option>
        <option value="weekly">yesterday</option>
        <option value="weekly">weekly</option>
        <option value="monthly">monthly</option>
        <option value="yearly">yearly</option>
    </select>
</span>

Add this event:

$('#spnSel').mouseover(function () {
   $('#yosh').prop('disabled', false);
});

You can do this for the text boxes as well. This may not be the best solution/approach but it will do the job.

like image 157
Sang Suantak Avatar answered Oct 20 '22 09:10

Sang Suantak


the syntax is

$('formelement').attr('disabled',true);

or to re-enable

$('formelement').removeAttr('disabled');
like image 20
jondavidjohn Avatar answered Oct 20 '22 07:10

jondavidjohn