Showing posts with label JQuery. Show all posts
Showing posts with label JQuery. Show all posts

Thursday, January 12, 2012

live() in Jquery

Once,the dom tree is created and loaded,If u are adding new elements dynamically,u may need to use live() to access those newly created elements.

Eg;  When a page is loaded,You got a select box with list of countries,
You selected a country and clicked on search button,
you got the list of all the state in that country,along with all the districts(in select box), for each starte in a row

When this page was loaded,list of all the country was loaded into select box,In the Dom it is added.
But when you selected country and clicked on search button,another table was loaded containing all the states,
This table is dynamic and it may not be loaded properly in DOM

Support in that table we have 5 rows,representing 5 state of India,each row is having 1 more select box,containing all the list of district in that state,Now you want to alert A msg on selecting any district

Here you need live()
Code Snippet 
-------------------
<script language="JavaScript">
$(document).ready(f1);
function f1()
{
$("#selDistrict").live("change", function() 
                             {
                                alert("I was clicked");
                              }
 }
</script>










  


   




Means this function() will be called when ever value of selDistrict  (id of select component) is changed

Wednesday, December 14, 2011

Problem while Attaching Datepicker to Dynamic Page

Datepicker is not attached in Dynamic form,whenever a new row is added.
-----------------------------------------------------------------------
If you are working with javascript,that dynamically creates a new row upon clicking Add button
& one of the field(dob) is datepicker,then usually u will find a problem

Newly created rows may not shown you datepicker on clicking the textfield.
This is because DOM object is not updated by javascript engine.


A way to overcome this problem is
When ever you click on add button,call a method,which once again applies datepicker to all the dob column

I will use jquery to do this stuff






DynamicForm.html
----------------------
<script src="js/jquery-1.5.2.min.js"></script>

<script type="text/javascript" src="js/jquery.datepick.js"></script>
<style type="text/css">
@import "css/jquery.datepick.css";
</style>

<script type="text/javascript">

$(document).ready(function()   //onpageload,when dom tree is ready execute this block
 {
       updateDatePicker();         //applies datepicker on existing form compents
  });

   function updateDatePicker()
   {
       //for all the input textfield whose name starts with startDate,apply datepicker
      $("input:text[name^=startDate]").datepick();//here iam referring component by its name,
//for all the input textfield whose id starts with endDate,apply datepicker
      $("input:text[id^=endDate]").datepick();//you can even refer by id
       
   }
function addRow()
{
//logic to dynamically add record,
updateDatePicker();
//call this method to apply datepicker again on the whole thing,so that newly created columns will also be //effected
}

</script>


<html>
        ------------------------
        ------------------------
    <input type=text name=startDate1>
    <input type=text id=endDate1 name=edate1>
    <input type=button onclick=addRow()>
</html>




Wednesday, November 30, 2011

Remove table using index number in jquery

Suppose you are sure that u need to remove 2nd table of the page,you can do this by using index
index starts with 0

$('table:eq(1)').remove();

The same concept can be applied for any elements

Friday, November 25, 2011

Adding Customized Dynamic FormValidation using jquery

DynamicCustomFormValidation.html
******************************

<script type="text/javascript" src="../jquery-1.6.2.js"></script>
<script type="text/javascript" src="../jquery.validate.js"></script>

<script>
$(document).ready(function() {
 $("#form2").submit(function()
  {
      var selError="";
      $("[id^=sport]").each(function()
        {
          //resetting the Color of Both checkbox,when clicked 2nd times onwards
          $(this).css({'background-color':'white'});
                 
             if($(this).val()=="none")
              {   
                 selError+=$(this).attr("id");
                 $(this).css({'background-color':'wheat'});
              }//if
        });//for each
       
      if(selError.length!=0)
      {
        $("#showmsg").html(selError);
        $("#showmsg").css({'background-color':'wheat'});
             
         return false;//stop page from being submitted
      }
         else return true;//submit the page
           
  });//submit()
}); //onready()
   
</script>
<body>
        <form id="form2">
                <select name="sport1" id="sport1">
                    <option value="none">Select a sport</option>
                    <option value="baseball">Baseball</option>
                        <option value="basketball">Basketball</option>
                    <option value="soccer">Soccer</option>
                    <option value="football">Football</option>
                </select> <br>
               
                <select name="sport2" id="sport2">
                    <option value="none">Select a sport</option>
                    <option value="cricket">cricket</option>
                        <option value="badminton">badminton</option>
                    <option value="swimming">swimming</option>
                    <option value="volleyball">VolleyBall</option>
                </select>
                <br>
            <input  type="submit" value="Submit" id="submitBtn">
        </form>
       
        <div id="showmsg" width=200></div>
</body>
*********************************Ends********************************************
















Tuesday, November 22, 2011

Creating Dynamic Form Using JQuery

The Objective of the Application is to create a dynamic form
  • clicking + button gives u a new row beneath it,
  • each row has unique id for each element
  • New row is generated by cloning the selected component,so all the value present in the previous component
  • Also gets cloned,which we have to remove("ChangeId" button does the same thing).
  • Change ID button assigns unique value to all component as well as removes the cloned data from component
  • "CheckID" button is given to check the id of each row,so that you can know the id was really changed.(Useful if you are not using debugger like firebug)
  • Select All the checkbox which you want to delete,click on delete btn,it will delete the row for you
Steps
****
click on CheckID to know the id of each component
Enter values to any rows components,and click its + symbol
Click "ChangeId" Btn to  change id,and previous cloned value removed
Again click on "CheckMe" to check the new id created for the newly inserted row
Select some checkbox and click on "Delete" to delete the row
 Folder Structure
************





Note:You can take any .jpg file instead of png's,make sure the height and width is appropriate

DynamicForm.html
***************

<html>
<head>
    <title></title>
    <script src="script/jquery-1.4.2.min.js"></script>

    <script type="text/javascript">
    var index=1;
    var flag=5;//because i have only 5 rows initially,so the counter for 6th onwards
    $(document).ready(function()
     {
        index++;
       $("#btnDel").click(function(){
           $('input:checkbox:checked').each(function()
                   {
               var idno=$(this).attr("id").substring(3,4);//get access to current checkbox number
               alert(idno);
               $("#row"+idno).remove();
                   });
            // alert('deleteBtn():called');          
        });
    $("[id^=ref]").click(function()
            {
        //alert("clicked");
        flag++;
            //get the position where you want to insert table
            var pos=$(this).attr('id').substring(3,4);
            $("#row"+pos).clone(false).insertAfter("#row"+pos).attr({id:"row"+flag});
            $("#cbx1 last").attr({id:"cbx7"});
            });
$("#btnChk").click(function(){
     $("input").each(function(){
              alert($(this).attr("id"));
              });
            });
   
    $("#btnChangeId").click(function()
        {
       
        $("#row"+flag+" input:checkbox").attr({id:"cbx"+flag});
        $("#row"+flag+" input:text[id^=text]").attr({id:"text"+flag});
        $("#row"+flag+" input:text[id^=age]").attr({id:"age"+flag});

        //nullify the value
        /**
        $("#row"+flag+" input:checkbox").attr('selected', false);
        $("#row"+flag+" input:text[id^=text]").val("");
        $("#row"+flag+" input:text[id^=age]").val("");
        **/
        //another quickest Approach to nullify
        $("#text"+flag).val("");
        $("#age"+flag).val("");
        $("#cbx"+flag).attr({checked:false});
    });
  });
  
</script>
</head>

<body>

<form id="myForm">
<table id="dataTable">
<tr><td></td><td>Name</td><td>Age</td></tr>

<tr id="row1"><td><input type=checkbox name="cbx1" id="cbx1"></td><td><input type=text value="" id="text1"></td><td><input type=text value="" id="age1"></td><td><div id="ref1"><img src="image/plus1.png" height="10" width="10"></div></td></tr>

<tr id="row2"><td><input type=checkbox name="cbx2" id="cbx2"></td><td><input type=text value="" id="text2"></td><td><input type=text value="" id="age2"></td><td><div  id="ref2"><img src="image/plus1.png" height="10" width="10"></div></td></tr>

<tr id="row3"><td><input type=checkbox name="cbx3" id="cbx3"></td><td><input type=text value="" id="text3"></td><td><input type=text value="" id="age3"></td><td><div id="ref3"><img src="image/plus1.png" height="10" width="10"></div></td></tr>

<tr id="row4"><td><input type=checkbox name="cbx4" id="cbx4"></td><td><input type=text value="" id="text4"></td><td><input type=text value="" id="age4"></td><td><div id="ref4"><img src="image/plus1.png" height="10" width="10"></div></td></tr>

<tr id="row5"><td><input type=checkbox name="cbx5" id="cbx5"></td><td><input type=text value="" id="text5"></td><td><input type=text value="" id="age5"></td><td><div id="ref5"><img src="image/plus1.png" height="10" width="10"></div></td></tr>

 </table>
 <input type="button" id="btnDel" value="Delete"/>
 <input type="button" id="btnChk" value="CheckID"/>
 <input type="button" id="btnChangeId" value="ChangeID"/>
  
</form>
</body>
</html>