Basic jQuery syntax and simplified AJAX
|
How to use DOM equivalent code withe JQuery.
How to get HTML fields value.
Text field: $(‘#name’).val();
Drop Down: $(‘#category option:selected’).val(); Radio Or Check Box: $(‘#checkid’).attr(‘checked’); // return true if checked other wise false; HTML content : $(‘#div’).html(); // same as innerHTML of DOM How to set value of HTML fields.
Text field: $(‘#name’).val(‘test’); Drop Down: $(‘#category option:selected’).val(‘ABC’); Radio Or Check Box: $(‘#checkid’).attr(‘checked’,true); or $(‘#checkid’).attr(‘checked’,false); HTML content : $(‘#div’).html(‘<table><tr><td>Test</td></tr></table>’); Suppose we have to add some event to more than one element then we can do that by element id one by one otherwise we can do that by class name. we give same class to those elements and write just one line JQuery to add an event to multiple elements.
|
<a href="#" title="Auto-Notify" class="basic1" id="test1"> <a href="#" title="Send to a Friend" class="basic1" id="test2"> $(document).ready(function () { $('.basic1').click(function (e) { e.preventDefault(); alert('hi'); }); }
$(document).ready(function () { $('#test1').click(function (e) { e.preventDefault(); alert('hi'); }); $('#test2').click(function (e) { e.preventDefault(); alert('hi'); }); }
$(function() { var _height=$('.contentArea').innerHeight(); $('.leftCol').css('height',_height); $('.rightCol').css('height',_height); });
You just need to determine which method you like GET or POST and you done with your AJAX .
$.post( 'send-to-friend.php', {your_name:$('#your_name').val(), your_email:$('#your_email').val() comments:$('#comments').val() } ,function(result){ $('#div').html(result); //here you call your function based upon ajax response. }); another way you can do this function LoadCategoryToEdit(cat_id) { $.post( 'get_categories_page.php', {cat_id:cat_id} ,LoadCategoryCallback ); } function LoadCategoryCallback(result) { $('#divCategoryForm').html(result); ShowCategoryForm(); } If you send the result in JSON format then $.get( 'send-to-friend.php', {your_name:$('#your_name').val(), your_email:$('#your_email').val() comments:$('#comments').val() } ,function(result){ result = JSON.decode(result); //here you call your function based upon ajax response. });