Convert HTML Form to JSON


Option -1:
function form_to_json(selector) {
    var ary = $(selector).serializeArray();
    var obj = {};
    for (var a = 0; a < ary.length; a++) obj[ary[a].name] = ary[a].value;
    return obj;
}

//Example
var mydata = form_to_json('#form');
JSON.stringify(mydata);
---------------------------------------------------------

Option-2:
Using Third party JQuery plugin- http://maxatwork.github.io/form2js/

<script src="form2js.js"></script>
<script src="jquery.toObject.js"></script>

var data = $('#form').toObject({skipEmpty:false});
JSON.stringify(data)
Advertisements

HTML Radio Button & JQuery

 <input type="radio" name="ABC" value="a b c" />
 <input type="radio" name="ABC" value="x y z" />
var radioValue=  $('input[type=radio][name=ABC]:checked').val();
               
if (typeof radioValue=='undefined') {
    alert('Please select ...');
    return;
}

CSS Styled Checkbox

Custom Checkbox Sample


       
/* Remove default checkbox */
[type="checkbox"]:not(:checked),
[type="checkbox"]:checked {
  position: absolute;
  left: -9999px;
  visibility: hidden; }

[type="checkbox"] {
  /* checkbox aspect */ }
  [type="checkbox"] + label {
    position: relative;
    padding-left: 35px;
    cursor: pointer;
    display: inline-block;
    height: 25px;
    line-height: 25px;
    font-size: 1rem;
    -webkit-user-select: none;
    /* webkit (safari, chrome) browsers */
    -moz-user-select: none;
    /* mozilla browsers */
    -khtml-user-select: none;
    /* webkit (konqueror) browsers */
    -ms-user-select: none;
    /* IE10+ */ }
  [type="checkbox"] + label:before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 18px;
    height: 18px;
    z-index: 0;
    border: 2px solid #5a5a5a;
    border-radius: 1px;
    margin-top: 2px;
    -webkit-transition: 0.2s;
    -moz-transition: 0.2s;
    -o-transition: 0.2s;
    -ms-transition: 0.2s;
    transition: 0.2s; }
  [type="checkbox"]:not(:checked):disabled + label:before {
    border: none;
    background-color: rgba(0, 0, 0, 0.26); }

[type="checkbox"]:checked + label:before {
  top: -4px;
  left: -3px;
  width: 12px;
  height: 22px;
  border-top: 2px solid transparent;
  border-left: 2px solid transparent;
  border-right: 2px solid #4CAF50;  /*Original code #ff4081*/
  border-bottom: 2px solid #4CAF50; /*Original code #ff4081*/
  -webkit-transform: rotate(40deg);
  -moz-transform: rotate(40deg);
  -ms-transform: rotate(40deg);
  -o-transform: rotate(40deg);
  transform: rotate(40deg);
  -webkit-backface-visibility: hidden;
  -webkit-transform-origin: 100% 100%;
  -moz-transform-origin: 100% 100%;
  -ms-transform-origin: 100% 100%;
  -o-transform-origin: 100% 100%;
  transform-origin: 100% 100%; }
[type="checkbox"]:checked:disabled + label:before {
  border-right: 2px solid rgba(0, 0, 0, 0.26);
  border-bottom: 2px solid rgba(0, 0, 0, 0.26); }

/* Indeterminate checkbox */
[type="checkbox"]:indeterminate + label:before {
  left: -10px;
  top: -11px;
  width: 10px;
  height: 22px;
  border-top: none;
  border-left: none;
  border-right: 2px solid #ff4081;
  border-bottom: none;
  -webkit-transform: rotate(90deg);
  -moz-transform: rotate(90deg);
  -ms-transform: rotate(90deg);
  -o-transform: rotate(90deg);
  transform: rotate(90deg);
  -webkit-backface-visibility: hidden;
  -webkit-transform-origin: 100% 100%;
  -moz-transform-origin: 100% 100%;
  -ms-transform-origin: 100% 100%;
  -o-transform-origin: 100% 100%;
  transform-origin: 100% 100%; }
[type="checkbox"]:indeterminate:disabled + label:before {
  border-right: 2px solid rgba(0, 0, 0, 0.26);
  background-color: transparent; }

[type="checkbox"].filled-in + label:after {
  border-radius: 2px; }
[type="checkbox"].filled-in + label:before,
[type="checkbox"].filled-in + label:after {
  content: '';
  left: 0;
  position: absolute;
  /* .1s delay is for check animation */
  transition: border .25s, background-color .25s, width .20s .1s, height .20s .1s, top .20s .1s, left .20s .1s;
  z-index: 1; }
[type="checkbox"].filled-in:not(:checked) + label:before {
  width: 0;
  height: 0;
  border: 3px solid transparent;
  left: 6px;
  top: 10px;
  -webkit-transform: rotateZ(37deg);
  transform: rotateZ(37deg);
  -webkit-transform-origin: 20% 40%;
  transform-origin: 100% 100%; }
[type="checkbox"].filled-in:not(:checked) + label:after {
  height: 20px;
  width: 20px;
  background-color: transparent;
  border: 2px solid #5a5a5a;
  top: 0px;
  z-index: 0; }
[type="checkbox"].filled-in:checked + label:before {
  top: 0;
  left: 1px;
  width: 8px;
  height: 13px;
  border-top: 2px solid transparent;
  border-left: 2px solid transparent;
  border-right: 2px solid #fff;
  border-bottom: 2px solid #fff;
  -webkit-transform: rotateZ(37deg);
  transform: rotateZ(37deg);
  -webkit-transform-origin: 100% 100%;
  transform-origin: 100% 100%; }
[type="checkbox"].filled-in:checked + label:after {
  top: 0px;
  width: 20px;
  height: 20px;
  border: 2px solid #ff4081;
  background-color: #ff4081;
  z-index: 0; }
[type="checkbox"].filled-in:disabled:not(:checked) + label:before {
  background-color: transparent;
  border: 2px solid transparent; }
[type="checkbox"].filled-in:disabled:not(:checked) + label:after {
  border-color: transparent;
  background-color: #BDBDBD; }
[type="checkbox"].filled-in:disabled:checked + label:before {
  background-color: transparent; }
[type="checkbox"].filled-in:disabled:checked + label:after {
  background-color: #BDBDBD;
  border-color: #BDBDBD; }

    <p>
        <input id="Checkbox1" type="checkbox" />
        <label for="Checkbox1">Checkbox</label>
    </p>

    <p>
        <input id="Checkbox2" type="checkbox" class="filled-in" />
        <label for="Checkbox2">Teletalk</label>
    </p>

Auto complete search textbox like google in a simple & easy way.

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>AutoComplete Box with jQuery</title>
    <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/themes/base/jquery-ui.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js"></script>
    
</head>
<body>
    <div class="demo">
        <div class="ui-widget">
            <label for="tbAuto">Enter UserName: </label>
            <input type="text" id="txtSearch" class="autosuggest" style="width:300px;" />
        </div>
        </div>
</body>
</html>

Place the script in head tag

<script type="text/javascript">
        $(document).ready(function () {
            SearchText();
        });
        function SearchText() {
            $(".autosuggest").autocomplete({
                source: function (request, response) {
                    $.ajax({
                        type: "POST",
                        contentType: "application/json; charset=utf-8",
                        url: "/Home/GetAutoCompleteData",
                        data: "{'username':'" + document.getElementById('txtSearch').value + "'}",
                        dataType: "json",
                        success: function (data) {
                            response(data.d);
                        },
                        error: function (result) {
                            alert("Error");
                        }
                    });
                }
            });
        }
    </script>
    public class HomeController : Controller
    {
        //
        // GET: /Home/
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult GetAutoCompleteData(string username)
        {

            List<string> result = new List<string>();

            result.Add("Hello");
            result.Add("World");
            return Json(new { d = result }, JsonRequestBehavior.AllowGet);
        }
    }

Working with HTML data- attribute

Sample HTML –

 
<td class="areaCell" data-expandstatus="false">Some text</td>

JQuery part –

 
//getter
var expandstatus = currentRow.attr('data-expandstatus');
//setter
currentRow.attr("data-expandstatus", "true"); 

//Or
//getter
var expandstatus = $(this).data('expandstatus');
//setter
//Functionable setter not found.

HTML Checkbox Tips and Tricks

//Checkbox checked change event handler
$('#CheckBox').click(function () 
{
    var thisCheck = $(this);

    if (thisCheck.is(':checked')) 
    {
       $('#span2').hide();
    }
    else
    {
       $('#span2').show();
    }
});

//Get checkbox value (checked/unchecked)
var checkBoxValue = $('#CheckBox').is(':checked');

HTML Select/Combo/DropDown tips and tricks

<select id="combo">
   <option value="1">One</option>
   <option value="2">Two</option>
</select>
<script>
   $(function () {
      var $Combo= $('#combo'); // assign to a variable

     //Get selected text 
         var selectedText = $('#combo>option:selected').html();
         //or,
         var selectedText = $('#combo option:selected').html(); 

      //Get selected value 
          var selectedVal = $('#combo>option:selected').attr('value');
          //or,    
          var selectedVal = $('#combo option:selected').attr('value');
      
         var selectedVal = $('#combo>option:selected').val();
         var selectedVal = $('#combo option:selected').val();

         var selectedVal = $($Combo, 'option:selected').attr('value');
         var selectedVal = $($Combo, 'option:selected').val();

      //Handle change event
      $regionCombo.change(function () {
                 //code to further...
             });
      });

      
     //Manipulate Selected Index (It cant be done with jquery)
     var $Combo = document.getElementById('combo');
     var selectedIndex = $Combo.selectedIndex; //getting selected index
     $Combo.selectedIndex = 2; //setting selected index

</script>

Full Page Web Modal

<style>
#displaybox {
	z-index: 10000;
	filter: alpha(opacity=50); /*older IE*/
	filter:progid:DXImageTransform.Microsoft.Alpha(opacity=50); /* IE */
	-moz-opacity: .50; /*older Mozilla*/
	-khtml-opacity: 0.5;   /*older Safari*/
	opacity: 0.5;   /*supported by current Mozilla, Safari, and Opera*/
	background-color:#000000;
	position:fixed; top:0px; left:0px; width:100%; height:100%; text-align:center; vertical-align:middle;
}
</style>

function clicker(){
	var thediv=document.getElementById('displaybox');
	if(thediv.style.display == "none"){
		thediv.style.display = "";
		thediv.innerHTML = "<a href='#' onclick='return clicker();'>CLOSE WINDOW</a>";
	}else{
		thediv.style.display = "none";
		thediv.innerHTML = '';
	}
	return false;
}

The hidden displaybox div
<div id="displaybox" style="display: none;"></div>


A link that will execute and return the clicker javascript function onclick: 
<a href='#' onclick='return clicker();'>Open Window</a>

Original Source of this article is here