Search This Blog

Showing posts with label Jquery. Show all posts
Showing posts with label Jquery. Show all posts

Thursday, October 18, 2012

Move elements from one list to another using jquery

This post will not help the C# gurus. Because they are already using this feature different techniques. But I hope this will be helpful for the PHP beginners :) . Now I am going to show you how you can move list items from one list box to another using simple jquery. Using jquery it is very simple than you think. :)

This is the Jquery portion:

 <script src="jquery-1.6.2.js"></script>

 <script>

 function move_forward()

 {

     //taking the selected items of list 1 and concatenating to a variable named forward_variable.

   var forward_variable='';

   $("#list1 :selected").each(function(){

       forward_variable+="<option value='"+$(this).val()+"'>"+$(this).html()+"</option>";

       $(this).remove();  

   });

     //Now appending the selected firs list's element to the list 2.

   $("#list2").append(forward_variable);

     //Sorting the list 2 so that it shows the list alphabetically

   sort_element("list2");

 }

 function move_backward()

 {

   var backward_variable='';

     //taking the selected items of list 2 and concatenating to a variable named backward_variable.

   $("#list2 :selected").each(function(){

       backward_variable+="<option value='"+$(this).val()+"'>"+$(this).html()+"</option>";

       $(this).remove();  

   });

    //Now appending the selected firs list's element to the list 1.

   $("#list1").append(backward_variable);

     //Sorting the list 2 so that it shows the list alphabetically

   sort_element("list1");

 }

 //Sorting function to sort the elements

 function sort_element(id)

 {

   var select_element = $('#'+id);

   var options_element = select_element.children('option').get();

   options_element.sort(function(a, b) {

     var compA = $(a).text().toUpperCase();

     var compB = $(b).text().toUpperCase();

     return (compA < compB) ? -1 : (compA > compB) ? 1 : 0;

   })

   $.each(options_element, function(index, items) { select_element.append(items); });

 }

 </script>


Now comes the html portion.
 <select multiple="multiple" name="list1" id="list1" style="width:200px">

   <option value="1">Ecstasy</option>  

   <option value="2">Tanjim</option>

   <option value="3">Rex</option>

   <option value="4">Artisti</option>

   <option value="5">Infinity</option>

 </select>

 <input type="button" name="forward" id="forward" onclick="move_forward()" value=">>"/>

 <input type="button" name="backward" id="backward" onclick="move_backward()" value="<<"/>

 <select multiple="multiple" name="list2" id="list2" style="width:200px">

 </select>



Simple is not it? Copy and paste the code. Run..

Sunday, September 30, 2012

How to get an element whose id starts with a particular pattern

Picking an element using its id is very in jquery, is not it? But what if the situation is you have three text fields and every of their id starts with a particular pattern? For example suppose you are working with three text fields

 <input type='text' name='properties_value' id ='properties_value_1' />  
 <input type='text' name='properties_value' id ='properties_value_2' />  
 <input type='text' name='properties_value' id ='properties_value_3' />  
 <input type='button' onclick='take_all_the_input();' value='Show'/>  

Now you want to work on the every text fields value using their id's. What will do? are u planning to write code for each and every text fields like the below???

 <script>  
 function take_all_the_input()  
 {  
    var input1 = $("#properties_value_1").val();  
    var input2 = $("#properties_value_2").val();  
    var input3 = $("#properties_value_3").val();  
   // I assume you just want to alert the values  
   alert(input1+' ' +input2+' '+input3);  
 }//end of function  
 </script>  
The above code will work but that is a very novice approach and will not work if you do not know exactly how many text fields will be there. That means if you have generate the text fields dynamically in that case you cant fix the number of text fields. Now come to the solution. The better solution (I am not telling it best but better because after reading this post you may write in your blog even better solution than my solution :) ). So the better solution is go through every text field's id that starts with a particular pattern (in this case 'properties_value_') since every id has started with 'properties_value_' and work on those values.

The code will be
 <script>  
 function take_all_the_input()  
 {  
   $("input[id^='properties_value_']").each(function()  
    {  
     // I assume you just want to alert the values  
     alert($(this).val());  
    }  
 }//end of function  
 </script>  

See we have used $("input[id^='properties_value_']").each(function() {..... Here you are going to traverse each of the id that starts with 'properties_value_' How are taking the id that starts with a particular pattern? using ^= . So this will give the attributes that starts with some pattern... On the other hand the $= will help you to find the ids that ends with a particular pattern.

Hope this helps.. Happy coding.


Saturday, September 8, 2012

Image Preview On FileUpload Change


 Add jquery  reference in 'head' tag

jquery-1.4.3.min.js

 Add Image and FileUpload control in html :

                         <table >
                 <tr>
                    <td>
                        <span >Logo Path</span>
                    </td>
                    <td>
                        <input name="FileUpload1" id="FileUpload1" onchange="showPreview(this)" style="width:260px;" type="file">
                    </td> 
                </tr>
                       </table>
                    <div >
                      <img id="imgLogo" alt="" src="" style="width: 77px; height: 66px" />
                  </div>
 Write Javascript  Function:

<script language="javascript" type="text/javascript">


        function showPreview(ele) {
            $('#imgLogo').attr('src', ele.value); // for IE
            if (ele.files && ele.files[0]) {
                var reader = new FileReader();
                reader.onload = function(e) {
                $('#imgLogo').attr('src', e.target.result);
                }
                reader.readAsDataURL(ele.files[0]);
            }
        }

</script>

Monday, September 3, 2012

Jquery:ToolTip

Add New js file in the js folder named  'tltip.js'and write some code

(function($) {
    $.fn.extend({
        tltip: function(options) {
        var settings = $.extend({ delay: 300, pause: 5000, animationSpeed: 500 }, options);
            return this.each(function() {
                var control = $(this);
                var iconDirection = 'left';
                var toolTipContainer = $('<div class="tltip-container"><div class="tltip-body">' + control[0].attributes["tltitle"].nodeValue + '' +
                                        '<img   class="tltip-close"   /></div><div class="tltip-icon"></div></div>');

                control.before(toolTipContainer);
                var delay = settings.delay;
                var toolTip = toolTipContainer;
                toolTip.css({ display: 'none' }).find('div').css({ display: 'none', opacity: 0 });
                var toolTipBody = $('.tltip-body', toolTipContainer);
                var toolTipIcon = $('.tltip-icon', toolTipContainer);

                var interval;
                control.mouseover(function() {
                    var position = $(this).offset();
                    var left = position.left;
                    var top = position.top;

                    left += $(this).outerWidth();

                    toolTipBody.css('left',12);
                    toolTipIcon.css('left', 0);

                    toolTip.css({ left: left, top: top });
                    interval = setTimeout(function() {
                        showToolTip(toolTip);
                    }, delay);
                }).mouseout(function() {

                    hideToolTip(toolTip);

                }).keydown(function() {
                    clearTimeout(interval);
                });

                $('.tltip-close', toolTipContainer).click(function() {
                    hideToolTip(toolTip);
                });

                function showToolTip(toolTip) {
                    toolTip.css({ display: '' })
                    .find('div').css('display', '')
                    .stop(false, true)
                    .animate({ opacity: 1 }, settings.animationSpeed, function() {
                        setTimeout(function() {
                            hideToolTip(toolTip);
                        }, settings.pause);

                    });
                }


                function hideToolTip(toolTip) {

                    toolTip.css({ display: 'none' })
                    .find('div')
                    .stop(false, true)
                    .animate({ opacity: 0 }, settings.animationSpeed, function() {
                        $(this).css('display', 'none');
                    });
                }

            });
        }
    });
})(jQuery);


CSS:
Add new css files named 'tltip.css' and write code..

.tltip-container
{
    height: 70px;
    width: 252px;
    position: absolute;
}
.tltip-body
{
     color: White;
    font-weight: bold;
    font-family: Arial;
    font-size: 10px;
    width: 220px;
    height: 39px;
    padding: 10px;
    padding-right:20px;
    position: relative;
    float: left;
    -webkit-border-radius: 4px;
    -moz-border-radius: 4px;
    border-radius: 4px;
    behavior: url(PIE.htc);
    background-color: #D5AF1C;
}
 

.tltip-icon
{
    width: 0;
    height: 0;
    border-style: solid;
    border-width: 6px;
    position: absolute;
    margin-top: 12px;
    border-color: transparent #D5AF1C transparent transparent;
}

.tltip-close
    {
    background:url(../images/x.png) no-repeat;
    width:25px; height:29px;
    display:inline; z-index:3200;
    position:absolute;
    top:-12px;
    right:-10px;
    cursor:pointer;
}

Add close image:

In html code:

Take reference in 'html' tag
 css/tltip.css
 js/jquery-1.4.3.min.js
 js/tltip.js


Write javascript code in html page

 <script type="text/javascript">
        $(document).ready(function () {
        $("[tltitle]").tltip();
        });
    </script>




 Add one div and three input box with new attribute named 'tltitle'

       <div id="demo">
            <input type="text" tltitle="Tooltip1." id="purple" />
            <br />
            <input type="text" tltitle="Tooltip2" id="green" />
            <br />
            <input type="text" tltitle="Tooltip3" id="yellow" />
           
        </div>



Sunday, September 2, 2012

Press Enter key and change Tab

Write  javascript code into js file named jskeypresstab.js.

$(':text').live("keypress", function(e) {
    /* ENTER PRESSED*/
    if (e.keyCode == 13) {

        /* FOCUS ELEMENT */
        var inputs = $(this).parents("form").eq(0).find(":input");
        var idx = inputs.index(this);
        if (idx == inputs.length - 1) {
            inputs[0].select()
        } else {
            inputs[idx + 1].focus(); // handles submit buttons
            inputs[idx + 1].select();
        }

        return false;
    }
});

$('select').live("keypress", function(e) {
    /* ENTER PRESSED*/
    if (e.keyCode == 13) {
        /* FOCUS ELEMENT */
        var inputs = $(this).parents("form").eq(0).find(":input");
        var idx = inputs.index(this);
        if (idx == inputs.length - 1) {
            inputs[0].select()
        } else {
            inputs[idx + 1].focus(); // handles submit buttons
            inputs[idx + 1].select();
        }

        return false;
    }
});


$('textarea').live("keypress", function (e) {
        /* ENTER PRESSED*/
        if (e.keyCode == 13) {

            /* FOCUS ELEMENT */
            var inputs = $(this).parents("form").eq(0).find(":input");
            var idx = inputs.index(this);
            if (idx == inputs.length - 1) {
                inputs[0].select();
            } else {
                inputs[idx + 1].focus(); // handles submit buttons
                inputs[idx + 1].select();
            }

            return false;
        }
    });

In html code:

Add reference in head code;

jquery-1.4.4.min.js
jskeypresstab.js

Saturday, September 1, 2012

Ajax Busy

Take new js file in your project named jsbusy.js and add javascript code into this files.

$(function() {
    $('body').append('<div id="jBusy"><p id="ajaxBusyMsg">Please wait...</p></div>');
});
//  bound to ajax start/stop document events
$(document).ajaxStart(function() {
    $('#jBusy').show();
}).ajaxStop(function() {
    $('#jBusy').hide();
});

In Html page:

Take reference into head tag:

jquery-1.4.3.min.js
jsbusy.js

CSS:

Write style into CSS file and take reference into head tag

#jBusy{
  display: none;
  margin: 0px 0px 0px -50px; /* left margin is half width of the div, to centre it */
  padding: 30px 10px 10px 10px;
  position: fixed;
  left: 50%;
  top: 40%;
  width: 200px;
  height: 100px;
  text-align: center;
  background: #e8e8e8 url(images/ajax-loader.gif) no-repeat center center;
  border: 1px solid #000
}

Add image into images folder



Wednesday, August 29, 2012

Jquery:Popup-2

 Jquery Reference:
 
    jquery-1.4.2.min.js
    jquery.simplemodal-1.4.2.js

Html Code For Popup Div:

<div id="basic-modal-content"  style="height: 323px">
                        <div id="detaildata" style="overflow: auto;height: 320px">
                        </div>
</div>

CSS:
#basic-modal-content {display:none;}

/* Overlay */
#simplemodal-overlay {background-color:#000; cursor:wait;}

/* Container */
#simplemodal-container {height:360px; width:600px; color:Black; background-color:#78C4FF; border:4px solid #444; padding:12px;}
#simplemodal-container .simplemodal-data {padding:8px;}
#simplemodal-container code {background:#141414; border-left:3px solid #65B43D; color:#bbb; display:block; font-size:12px; margin-bottom:12px; padding:4px 6px 6px;}
#simplemodal-container a {color:Lime;}
#simplemodal-container a.modalCloseImg  {background:url(x.png) no-repeat; width:25px; height:29px; display:inline; z-index:3200; position:absolute; top:-15px; right:-16px; cursor:pointer;}
#simplemodal-container h3 {color:#84b8d9;}

Add Image:




HTML Button for popup:


<input value="popup" onclick="divpopup();return false;" type="submit">
 
javascript Function: 
 
       function divpopup()  {
        $('#basic-modal-content').modal();           
       }

Jquery:Popup-1

Jquery Reference:
       jquery-1.4.2.min.js
       jquery.helper.js

CSS:

/* Panel styles start */
.popup
{
    font-family: arial,helvetica,clean,sans-serif;
    font-size: small;
    padding: 2px 3px;
    display: block;
    position: absolute;
    z-index:1;
}

.container
{
    border: solid 1px #808080;
    border-width: 1px 0px;
}

.header
{
    background:url(images/top-bg.gif) repeat-y;
    border-color: #808080 #808080 #ccc;
    border-style: solid;
    border-width: 0px 1px 1px;
    padding: 3px 10px;
    text-align:left;
}

.popup .header .msg
{
    font-weight: bold;
}        

.popup .body
{
    background-color: #f2f2f2;
    border-color: #808080;
    border-style: solid;
    border-width: 0px 1px;
    padding: 10px;
}

.popup .body .msg
{
    font-family: arial,helvetica,clean,sans-serif;
    font-size:small;
   
    background: url(sprite.png) no-repeat 0px -1150px;     
    float: left;
    padding-left: 22px;
   /* */
} 
.popup .footer
{
    background: url(sprite.png) repeat-x 0px -200px; 
   

    text-align:right;
    padding-top: 4px;
    padding-bottom: 4px;
    padding-right: 8px;
}

.popup .close
{
    right: 7px;  
    background: url(sprite.png) no-repeat 0px -300px;
    width: 25px; 
    cursor: pointer;   
    position: absolute;
    top: 7px;    
    height: 15px;
}

.button_popup
{
    cursor: pointer;
    border-width:1px;
    background-color:#f2f2f2;
    border-color:#808080;
}


.bodybox {
    background:#FFF!important;
    padding:10px;
   
    -moz-border-radius: 5px;
    -webkit-border-radius: 5px;
    -khtml-border-radius: 5px;
    border-radius: 5px;
    behavior: url(border-radius.htc);
   
    -moz-box-shadow: 0 0 5px #000; /* Firefox */
     -webkit-box-shadow: 0 0 5px #000; /* Safari, Chrome */
     box-shadow: 0 0 5px #000; /* CSS3 */
      behavior: url(ie-css3.htc);
      width:96%;
      margin-left:auto;
      margin-right:auto;
}
/* Panel styles end */





HTML Code For Popup Div:

     <div id="divSimplePopup" class="popup" style="display: none; width: 540px; top: 278px;
            left: 151px; height: 334px;">
            <div class="container">
                <div class="bodybox">
                    <div id="g"  style="height: 289px; overflow: auto;" class="footer">
                        <div class="header" style="background-color: #003399">
                            <span id="lblPopupHeader" class="msg" style="color: #FFFFFF; font-family: Arial, Helvetica, sans-serif">
</span>
                        </div>
                        <div id="detaildata" style="overflow: auto;height: 239px">
                        </div>
                    </div>
                    <div class="footer">
                        <input class="button" id="btnCancel" onclick="$('#divSimplePopup').hideModal(); return false;"
                            type="submit" value="Cancel" />
                    </div>
                    
                </div>
            </div>
        </div>

HTML Button for popup:

<input value="popup" onclick="divpopup();return false;" type="submit">
 
javascript Function: 
 
       function divpopup()  {
        $('#divSimplePopup').showModal();           
       }
 

Tuesday, August 28, 2012

Get selected checkbox row from datagrid by jquery

HTML Code:


<asp:DataGrid ID="dgdProduct" runat="server"  >
                 <Columns>
                         <asp:TemplateColumn>
                                <ItemTemplate>
                                      <asp:CheckBox ID="chkP" runat="server"     />
                                     <input id="hdID" type="hidden"  value='<%# Eval("pCode") %>' />
                                    </ItemTemplate>
                           </asp:TemplateColumn>
                           <asp:BoundColumn DataField="pDescription" HeaderText="Product Description">
                              </asp:BoundColumn>
                             <asp:BoundColumn DataField="Quantity" HeaderText="Quantity">
                            </asp:BoundColumn>
                          </Columns>
 </asp:DataGrid>

JQuery Code:

         var $table = $("#dgdProduct"),
              $headerCells = $table.find("thead th"),
              $rows = $table.find("tbody tr");
          var   rows = [];
          var isChecked;
          var pcode;
         var pDescription;
          $rows.each(function(row, v) {
              var $td = $(this).closest('tr').children('td');
              $td.find("input:checkbox").each(function() {
                  isChecked = $(this).attr('checked');
                
              });

              if ($td.length > 0) {
                  if (isChecked == true) {
                      pcode= $td.find('input:hidden[id$=hdID]').val();
                    pDescription=$td.eq(1).text() ;
                  }
              }
          });

Jquery:Load DropDown List

Jquery:Load DropDown List