Use “id” or “name” attribute in HTML component with jQuery

Sometimes we need to use “id” or  “name” attribute in our html components. I would like to show the difference between them through jQuery:

The HMTL

<input type="text" name="_inputName" id="_inputId" ></input>
<select type="text" name="_selectName" id="_selectId" ></select>
  1. Use “id” attribute with jQuery:
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js" type="text/javascript"></script>
    <script type="text/javascript"> 
    $(document).ready(function(){ 
    $('#_inputId').attr('disabled', true);
    $('#_selectId').attr('disabled', true);
    }); </script>
  2. Use “name” attribute with jQuery:

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js" type="text/javascript"></script>
    <script type="text/javascript"> 
    $(document).ready(function(){ 
    $('input[name="_inputName"]').attr('disabled', true);
    $('select[name="_selectName"]').attr('disabled', true);
    }); 
    </script>

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s