Thursday, January 21, 2016

jQuery remove data attribute and add data attribute



Suppose we have data attribute id, value 76
<tr class="worksheet" data-id="76"></tr>
To remove data attribute id in  jQuery
         $('.worksheet').removeData("id");
To add data attribute id in  jQuery
       $('.worksheet').attr("data-id",""); 
  Get value:
   $('.worksheet').data("id");
 Set value
   $('.worksheet').data("id",""); 
  For some reason set value not working well, I use remove data attribute and add data attribute instead.

Tuesday, January 19, 2016

disabled input in jQuery serializeArray();




If an input has attribute disabled, it will not be included in   jQuery serializeArray();
To make it included in jQuery serializeArray(),  we first make it enabled and disabled again
 Example
                var disabled_attr =$('#instructorname').attr("disabled");
                if(disabled_attr=='disabled') $('#instructorname').removeAttr("disabled");
                var postData = $("form").serializeArray();
                if(disabled_attr=='disabled')  $("#instructorname").prop("disabled", true);

To get serializeArray into post bvalue
         dataObj = {};
         for (i=0; i<len; i++) {
           dataObj[postData[i].name] = postData[i].value;
         }

Add more value, some of them from url get
                postData.push({
                        name: "funct",
                        value: "saveGuidelineInfo"
                }, {
                        name: "sid",
                        value: $.url.param('sid')
                },{
                        name: "term",
                        value: $.url.param('term')
                },{
                        name: "instr_section",
                        value: $.url.param('instr_section')
                },{
                        name: "cid",
                        value: $.url.param('cid')
                },{
                        name: "pad_id",
                        value: $.url.param('pad_id')
                }
                );

Final post data
              $.post("../_ajaxParts/TA/SITAContractView.php", postData, function(data) {
                        formatJson(data, 'div#notice', null);
                           });

Monday, January 11, 2016

$(document).on('click', check which class is clicked for multiple classes




For example, jquery, click:
 $(document).on('click',  '.sendmail00, .sendmailq',function(){
I want to check which class is clicked? .sendmail00 or .sendmailq?
We can check its parent to contain this class or not.
Examples, suppose both of classes have parents td,
    $(document).on('click',  '.sendmail00, .sendmailq',function(){
                     var $td = $(this).parents('td');

      if($td.find('.sendmailq').html()==null ||$td.find('.sendmailq').html()==undefined)  
        alert('Not contain class  .sendmailq');
      else
        alert('Contain class  .sendmailq');
 });

click on box, but not a link in the box, jquery



I have link inside a box
<div class='term'><a href='#'' class='link'>My Link</a></div>
I have click on class term. When I click the link, I want the class 'term' not trigger a click.
used:
              if (event.target.nodeName == 'A') 
Example:
            $('.term').on('click',  function(event){
              if (event.target.nodeName == 'A') return true;

          } 
 Another method:
 e.stopPropagation();
Example 
$('.term a').click( function(e) {
    e.stopPropagation();
});
Reference:
http://stackoverflow.com/questions/3838758/select-the-div-but-not-the-link-inside-it-jquery-question 

Saturday, January 9, 2016

$(document).ready(function() {}); and $(function() {});



What does "$(function(data){"  mean  in jQuery?
It is the same as $(document).ready(function () {
  • $(document).ready(function() {});
  • $(function() {});
The two statements are actually the exact same. So the second call is just a shortcut for the first.

Wednesday, January 6, 2016

php, escape quotes in html input value



In form
 <input type="text" name="firstname" value="<?php echo $_POST['firstname']; ?>" />
You may find if  

$_POST['firstname']=O'test may not work.
We need to use  htmlspecialchars or  htmlentities()

But we need to use  ENT_QUOTES to esacpe single, i.e.
 htmlspecialchars($_POST['firstname'],ENT_QUOTES) 

More about htmlspecialchars:
  • '&' (ampersand) becomes '&amp;'
  • '"' (double quote) becomes '&quot;' when ENT_NOQUOTES is not set.
  • "'" (single quote) becomes '&#039;' (or &apos;) only when ENT_QUOTES is set.
  • '<' (less than) becomes '&lt;'
  • '>' (greater than) becomes '&gt;'