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

Friday, November 11, 2011

multiple file upload with jQuery

Within forms, many times we need to empower users to enter one or more files. In some cases you may need to perform the upload of multiple files, without knowing in advance how they might be.
Limit the number of inputs for uploading files is sometimes not the best choice, so we could give the user a chance to put other files in his complete autonomy.

This post explains how to use jQuery without any plugins for creating html for inserting multiple fileupload.

We start from a simple html page that contains only those elements essential for this application:


<!DOCTYPE html>
<html>
<head>
    <title>Multiupload file</title>
    <script type="text/javascript" src="Scripts/jquery-1.6.2.min.js"></script>
    <script type="text/javascript" src="Scripts/JScript.js"></script>
</head>
<body>
      
    <div class="container">
        <button id="btAddFileUpload" type="button">Add file</button>
        <div id="fileUp1">
            <input id="File1" type="file" />
            <button id="btDelete1" type="button" value="1" onclick="javascript:deleteUpload(value)">Delete</button>        
        </div> 
      
        <input id="Submit1" type="submit" value="Submit" />
    </div>
  
</body>
</html>
 

I copied into my "Scripts" directory latest jQuery library version, but you can refer it in the classic mode by Google:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>

I wrote my JavaScript code in an external file called "JScript.js" and referenced in html script tag. This is the code:


$(document).ready(function () {
    $('#btAddFileUpload').click(add);
});

//upload file tags counter
var count = 1;


//add fileupload method
function add() {

    //upload file tags increase
    count++;   
   
    //clone div tag with all children elements
    var copied = $('#fileUp1').clone(true);

    //increase all children tags ids
    $('input:file', copied).attr('id', 'File' + count);
    $('button', copied).attr(
        { id: 'btDelete' + count,
          value: count
        }
    );

    //increase div tag id
    $(copied).attr('id', 'fileUp' + count);

    /* another mode to change attribute names
    copied.children('input').attr('id', 'File' + count);
    copied.children('button').attr('id', 'btDelete' + count);
    */

    //append copied div and relative children tags in container div, but before submit button
    $(copied).appendTo('.container').insertBefore('#Submit1');
}


//delete method fileupload method
function deleteUpload(id) {

    //remove file upload (parent div and childrens)
    if (count > 1) {

        //decrease total amount of uploadfile tags
        count--;

        var nameElemToDelete = '#fileUp' + id;
        $(nameElemToDelete).remove();
    }


    //to avoid holes, recalculating all the ids after the removed one
    for (indx = id; indx <= count; indx++) {
           
        var inputDiv = $('#fileUp' + (parseInt(indx) + 1));
        $(inputDiv).attr('id', 'fileUp' + indx);
       
        $('input:file', inputDiv).attr('id', 'File' + indx);
        $('button', inputDiv).attr(
            { id: 'btDelete' + indx,
              value: indx
            }
        );
    }
}




At the begin, I call the $(document).ready jQuery statement to join the add function to add button at the startup (page entirely loaded).
Each time you click on the button "btAddFileUpload", all you clone the "
" tag including its children, renaming the id attributes with a new name including the number increases by one.
All this is done using the jQuery methos "clone" on an object (tag) that you want to replicate. 


With "deleteUpload" function you can delete the same tag (and all its childrens) and recalculate all ids to avoid holes. This was done to iterate over all the inputs file without any problems.

Wednesday, October 12, 2011

jQuery Animated Slider


Light-Weight jQuery Animated Slider

Today’s tutorial is a brief look at the jQuery animate function and how this can be used to create a very quick and easy content slider from an unordered list and a few lines of jQuery.
Whilst there are a lot of plugins out there for creating slick jQuery sliders sometimes all you need is a few lines a code to come up with your own features and in fact this simple code snippet is only 580 bytes!

JQuery Menu Slider


jQuery Tutorial – Create a jQuery Menu Slider

jQuery Menu Slider

Monday, January 31, 2011

Hide/Show paragraph by button click in Jquery

<!DOCTYPE html>
<html>
<head>
<style>
p { background:#DAA520; font-weight:bold;}
</style>
<script src="jquery-1.4.2.js" type="text/javascript"></script>
</head>
<body>
<button id="butt1">Click to hide Paragraph</button>
<button id="butt2">Click to show Paragraph</button>
<p>This will Hide after clicking button</p>
<p>It will hide too....................</p>
<script>
$("#butt2").hide();
$("#butt1").click(function () {
$("p").hide("slow");
$("#butt1").hide("slow");
$("#butt2").show("slow");
});
$("#butt2").click(function () {
$("p").show("slow");
});
</script>
</body>
</html>

How to Separating custom code in different file in Jquery



jqCustom.html
<html>
<head>
<title>The jQuery Example</title>
<script type="text/javascript" src="jquery-1.4.2.js">
</script>

<script type="text/javascript" src="custom.js"></script>
</head>
<body>
<div id="mydiv">
Click here to see a dialogue box.
</div>
</body>
</html>






custom.js
$(document).ready(function() {
  $("div").click(function() {
  alert("Hello Friends!");
  });
});

We can include "custom.js" file as :
<script type="text/javascript" src="custom.js"></script>


Display alert box on clicking link In JQuery

<html>
<head>
<script type="text/javascript" src="jquery-1.4.2.js">
</script>

<script type="text/javascript">
$(document).ready(function(){
$("a").click(function(event){
alert("Redirecting you to jQuery.com!");
});
});
</script>
</head>
<body>
<a href="http://jquery.com/">
Click here to know about jQuery</a>

</body>
</html>

Monday, January 17, 2011

Watermark Textbox using jquery

,jHere is the coding for Watermark inside a textbox. That is initially the message will show inside the textbox. Once you click on the textbox it will disappear. This is used by jquery.

CSS:

<style type="text/css" media="screen">
.water
{
font-family: Tahoma, Arial, sans-serif; 
color: blue; 
} 
</style>

  
SCRIPT: 
  
        <script src="js/jquery-1.2.6.min.js" type="text/javascript"></script> 
  
DESIGN: 
  <div>


<h2>

Watermarked TextBox </h2>

<br />
<asp:TextBox ID="txtFirstName" class="water" Text="Enter First Name" ToolTip="Enter First Name" runat="server"></asp:TextBox><br />
<asp:TextBox ID="txtLastName" class="water" Text="Enter Last Name" ToolTip="Enter Last Name" runat="server"></asp:TextBox> <br /> 
<asp:Button ID="btnSubmit" runat="server" Text="Submit" ValidationGroup="vgNews" /></div>

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Best Wallpapers collections