Search This Blog

Showing posts with label jquery. Show all posts
Showing posts with label jquery. Show all posts

Mar 20, 2013

DOM Manipulation: How to Enable / Disable Element in jQuery

What I love about jQuery is its effectiveness in dealing with DOM manipulation. You can easily select, add or remove classes in your html document. You can also manipulate html attributes on the fly. For example if you want to disable / enable element, all you have to do is to set disabled attribute to "disabled" using the following code.

/* to disable */ 
$('.myElement').attr('disabled', 'disabled'); 

To enable it just simply removed the added "disabled" attribute or you can assign an empty string value to disabled attribute.

$('.myElement').attr('disabled', '');  

OR

$('.myElement').removeAttr('disabled');  

Jan 3, 2012

Detecting IE Browser and It's version

jQuery approach $.browser.msie is the best way to detect if you are viewing in an Internet Explorer browser. In addition, you can also add another jQuery code to detect the version of the IE browser - $.browser.version. Detecting browser is important since there are some jQuery and other framework code that are not working proper in different browsers. In this manner, you can make conditional code depending on the user’s browser.

Using jQuery :
<script>
   // less than IE 8 
  if ($.browser.msie && $.browser.version.substr(0,1)<8) {
     //your code here 
   }
  // less than IE 7 
  if ($.browser.msie && $.browser.version.substr(0,1)<7) {
     //your code here 
  }
</script>
NOTE: Don't forget to Add jQuery library.

Using conditional comment:

<!--[if lte IE 6]>
<script type="text/javascript">
  var isRunningIE6OrBelow = true;
</script>
<![endif]-->

May 31, 2011

Magento: Solve jQuery Conflict

In default, Magento use Prototype framework for all processes that needs to be done using javascript. But most cases, developers use jQuery platform since it is easy to implement and understand. Thus, both of these framework use “$” in a function or in a variable name. These common feature result error or conflict when you try to use both in one single web site.

According to jQuery:

Many JavaScript libraries use $ as a function or variable name, just as jQuery does. In jQuery’s case, $ is just an alias for jQuery, so all functionality is available without using $. If we need to use another JavaScript library alongside jQuery, we can return control of $ back to the other library with a call to $.noConflict():

In order to solve the issue, you need include the following code in your head.html file. Here are the step by step guides in adding jQuery to Magento.

1. Download the latest jQuery library.
2. Put the jquery library in your skin directory. skin/frontend/default/your_theme/js/
3. Then, add the jquery using XML in your page.xml file. This is located in app/design/frontend/default/your_theme/layout/page.xml
4. Open your page.xml in an editor and look for “head” xml child element. Then add the following code.
5. IMPORTANT: To remove the conflict in other framework, add the following code in your head.phtml file

<script type="text/javascript" >
    jQuery.noConflict();
</script >

6. Done! You now solved issue between jQuery and other javascript library.

Mar 24, 2011

Simple PHP / jQuery Contact Form

My last PHP snippet is about creating PHP/MySQL Login Form for any particular PHP/MYSQL system. Today I will give you another snippet, a Simple PHP / jQuery Contact Form. Contact us is essential element when having a website especially when you’re catering products online. This is pre-requisite to all e-commerce website to make contact page in order for their customer reach them to ask question / for just sending feedback. In this post I will show you on how to create a simple PHP and jQuery Ajax Contact Form.

Step Summary

1. Create file contact.php.
2. Create file contact_send.php

Details

1. Create a file contact.php

- this includes your form with input field along with their corresponding fields.
CODE:

<form id="contact-form">
Name : <input class="text" name="contact-names" type="text" />
<span class="name-required"></span>
Email :<input class="text" name="contact-email" type="text" />
<span class="email-required"></span>
<div class="comment">
Comment :</div>
<textarea class="text-area" name="contact-comment"></textarea>
<span class="comment-required"></span>
<input class="send" id="submit-form" name="submit" type="submit" value="" />
</form>

2. The next step is to create a contact_send.php file

<?php
 $names = $_POST['names'];
 $email = $_POST['email_address'];
 $comment = $_POST['comment'];
 $to ='your@email.com';
 
 $message = "";
 $message .= "*Name: " . htmlspecialchars($names, ENT_QUOTES) . "<br>\n";
 $message .= "*Email: " . htmlspecialchars($email, ENT_QUOTES) . "<br>\n";
 $message .= "Comment: " . htmlspecialchars($comment, ENT_QUOTES) . "<br>\n";
 $lowmsg = strtolower($message);
  
 $headers = "MIME-Version: 1.0\r\nContent-type: text/html; charset=iso-8859-1\r\n";
 $headers .= "From: \"" . $names . "\" <" . $email . ">\r\n";
 $headers .= "Reply-To: " .  $email . "\r\n";
 $message = utf8_decode($message);  mail($to, "Note from the Contact Form", $message, $headers);
 
 if ($message){
   echo 'sent';
 }else{
    echo 'failed';
 }
?>

3. Then the last thing you need to do is to add the jQuery Ajax functions that will call your contact_send.php file. Edit your contact.php file and add the following code:

IMPORTANT:  Don't forget to add the jQuery javascript at the the top of this code.

<script>$(document).ready(function(){
$('#submit-form').click(function(){
   
 var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
 var names  = $('#contact-form [name="contact-names"]').val();  
 var email_address = $('#contact-form [name="contact-email"]').val();
 var comment  = $.trim($('#contact-form .contact-comment').val());
   
  var data_html ='' ;
   
   
  if(names == ""){
       $('.name-required').html('Please enter your name <br/>.');
     }else{
       $('.name-required').html('');
     }
     if(email_address == ""){
       $('.email-required').html('Your email is required.');
     }else if(reg.test(email_address) == false){
       $('.email-required').html('Invalid Email Address.');
     }else{
       $('.email-required').html('');
     }
     
     if(comment == ""){
       $('.comment-required').html('Comment is required.');
     }else{
       $('.comment-required').html('');
     }
     
   if(comment != "" && names != "" && reg.test(email_address) != false){
   
    data_html = "names="+ names + "&comment=" + comment + "&email_address="+ email_address;
    
    //alert(data_html);
    
     $.ajax({
      type: 'POST',
      url: 'contact_send.php',
      data: data_html,
      success: function(msg){
       if (msg == 'sent'){
        $('#success').html('Message sent!')  ;
        $('#contact-form [name="contact-names"]').val('');  
        $('#contact-form [name="contact-email"]').val('');
         $('#contact-form .contact-comment').val('');
        
       }else{
        $('#success').html('Mail Error. Please Try Again.!')  ; 
       }
      }
   });
   
    }
      
   return false;
  })
})
</script>

Validation Output

Jun 26, 2010

The best Javascript Framework?

 Here are some list of JavaScript Framework in the internet. What is your bet to be on TOP?

 1.Moo Tools - is an free Javascript framework that evolved from the simple function called moo.fx. The developers of moo.fx have slowly expanded this simple function into a fully functional framework.Moo Tools is known for the elegance style and effect in handling dynamic function in the web.
 
download:  Moo Tools
Sample demo: http://demos.mootools.net/



2. JQuery - is a JavaScript Library Framework, that simplifies the HTML document traversing, event handling, animating, and more . It allow web developer to  get more interaction to their sites . JQuery is used to add behavior to a web page.

download: jQuery
demo:http://jqueryui.com/





 3. Prototype - according to www.ostatic.com The Prototype JavaScript Framework is a JavaScript framework created by Sam Stephenson that provides an Ajax framework and other utilities. Prototype provides various functions for developing JavaScript applications. The features range from programming shortcuts to major functions for dealing with XMLHttpRequest. Prototype also provides library functions to support classes and class-based objects, something the JavaScript language does not have.
download: Prototype
API:http://www.prototypejs.org/api



4.Dojo Toolkit - according to the dojo toolkit site, Dojo saves you time, delivers powerful performance, and scales with your development process. It’s the toolkit experienced developers turn to for building great web experiences.



download: Dojo Toolkit
demo : http://www.dojotoolkit.org/demos/


5.  script.aculo.us - according to http://script.aculo.us/  script.aculo.us provides you with easy-to-use, cross-browser user interface JavaScript libraries to make your web sites and web applications fly.


download:script.aculo.us
demo: http://ndpsoftware.com/ScriptaculousEffectsDemo.php