Category: Javascript

  • Filter Builder

    Filter Builder


    Recently I have developed a Drag and drop dynamic filter which generates JSON. you can integrate into any Application no matter it is the iOS app, Android App or Web App.

    You just need to integrate this code into iFrame or Web view so that user will build his/her desire filter option using very easy drag and drop functionality. And at the end, you will get JSON which you need to pass to the server. And the server will parse this JSON and apply DB query based on JSON object. and the server will return the JSON string.

    So bye-bye to traditional fix filter. With my filter user have full flexibility to build his/her desire filter and very user-friendly.

    Soon I will release its downloadable version with proper documentation. As of now please play with this and let me know in the comment section what new feature you want to see in this to make it more user-friendly with more productive.

    Demo

  • Drag and drop object from one div to another using javascript

    Drag and drop object from one div to another using javascript

    How to drag and drop an object from one Div to another div using pure JavaScript?
     
    Yes. We can use HTML5’s power to drag and drop any object from one div to another using only JavaScript no other library like jQuery so that our webpage will get too heavy.
     
    What you need?

    1. 1 HTML page
    2. 1 image

    so lets start with basic.
    first of all create html page and write following code into that page:
     

    <html>
    <head>
    <title>Tj’s Solution for drag and drop</title>
    </head>
    <body>
    <div id="drop_container" ondrop="drop(event)" ondragover="DopHere(event)"></div>
    <img id="image_drag" src="https://www.tejasrana.com/wp-content/uploads/2015/06/TR.png" draggable="true" ondragstart="image_drags(event)">
    </body>
    </html>

    write this script to drag and drop
     

    <script>
    function DopHere(ev) {
        ev.preventDefault();
    }
    function image_drags(ev) {
        ev.dataTransfer.setData("text", ev.target.id);
    }
    function drop(ev) {
        ev.preventDefault();
        var data = ev.dataTransfer.getData("text");
        ev.target.appendChild(document.getElementById(data));
    }
    </script>

    Now we have one simple problem is that if that div have already one image and also its capacity to store one image then we have to set a validation. for that validation:
     

    <script>
    function hasImage(id) {
           var arrival_div=document.getElementById(id);
           var childElements = document.getElementById(id).childNodes;
           for (var i = 0; i < childElements.length; i++) {
             if (childElements[i].localName != null && childElements[i].localName.toLowerCase() == "img") {
               return true;
             }
           }  
           return false;
         }
    </script>

    This function will check that the target div have img tag or not. now we set condition before dropping.
    So our drop function will look like this:
     

    <script>
    function drop(ev,div_id) {
           ev.preventDefault();
           
           var hasImages =hasImage(div_id);
           var data = ev.dataTransfer.getData("text");
           if(hasImages==false){       
             var data = ev.dataTransfer.getData("text");
             ev.target.appendChild(document.getElementById(data));
           }else{
    alert(‘div already have an image.’);
    }
         }
    </script>

    So the complete output look like this with more improved:
     

    <html>
    <head>
    <title>drag and drop - From Tejas Rana (TejasRana.com)</title>
    <style>
    #drop_container{border:1px solid #000; width:160px; height:160px; padding:5px; float:left; margin:20px;}
    #dropback_container{border:1px solid #000; width:160px; height:160px; padding:5px;float:left; margin:20px;}
    </style>
    </head>
    <body>
    <iframe src="https://www.tejasrana.com/header-only/" frameborder="0" scrolling="no" style="border:0px; width:100%;"></iframe>
    <div id="drop_container" ondrop="drop(event,'drop_container')" ondragover="DopHere(event)"></div>
    <div id="dropback_container" ondrop="drop(event,'dropback_container')" ondragover="DopHere(event)"><img id="image_drag" src="https://www.tejasrana.com/wp-content/uploads/2015/06/TR.png" draggable="true"
    ondragstart="image_drags(event)"></div>
    <script>
    function DopHere(ev) {
        ev.preventDefault();
    }
    function image_drags(ev) {
        ev.dataTransfer.setData("text", ev.target.id);
    }
    function drop(ev,div_id) {
           ev.preventDefault();
         
           var hasImages =hasImage(div_id);
           var data = ev.dataTransfer.getData("text");
           if(hasImages==false){       
             var data = ev.dataTransfer.getData("text");
             ev.target.appendChild(document.getElementById(data));
           }else{
    alert('div already have an image.');
    }
       }
    function hasImage(id) {
           var arrival_div=document.getElementById(id);
           var childElements = document.getElementById(id).childNodes;
           for (var i = 0; i < childElements.length; i++) {
             if (childElements[i].localName != null && childElements[i].localName.toLowerCase() == "img") {
               return true;
             }
           }  
           return false;
         }
     
    </script>
    <iframe src="https://www.tejasrana.com/footer-only/" frameborder="0" scrolling="no"  style="border:0px; width:100%;"></iframe>
    </body>
    </html>

     
    [wpdm_package id=’2298′]