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

Jan 26, 2010

Show/Hide and Disable Click of Radio Buttons in JQuery

Here is a nice code snippet that can help you really while you work with radio buttons and jquery!

When you have a group of radio buttons, you might want to hide all of them when one is selected and submitted.

Here is the code that can hide the radio buttons for you: (assuming that the name of the radio button that you give to all of them is "answers")

$('input[name="answers"]').each(function(){
$(this).hide();
});


Yet another interesting code snippet that might help you while you work with radio buttons is as given below:

There are times when you feel like disabling the click of the radio button based on certain conditions. Following code helps you in disabling the click on the radio button when the radios are actually hidden and the corresponding texts are visible. (name of the radio buttons is given as "radios")

<br />$('input[name="radios"]').each(function() {<br />$(this).click(function(){ <br />// test if radio is hidden<br />if ( $(this).is(':hidden') ) { <br /> return false;<br /> } else {<br /> return true;<br /> }<br />});<br />});<br />

Jan 5, 2010

(function($){ }) JQuery : What It Really Is?

Today I happen to use the code snippet


(function($){ })(jQuery);


But, for around an hour, I was wondering as to what the above function really does? what is the use of wrapping functions like


$(this).submit(function(){}
$(document).ready(function(){} 


inside the above?

After a bit of research, I understood what it really does and that is awesome. This post further gives you a heads up on what


(function($){ })(jQuery);


is in detail.

The $ in


(function($){ })(jQuery);


can be considered to be prototype reference outside the function call. This $ becomes a jquery reference inside the function. as a result, even if this $ variable or function is being used by any other javascript library, this reference overwrites all and proves to be a nice jquery reference well within the function.

Generally you can expect to see all JQuery plug-ins to be written inside this function where $ is being passed as a parameter. Before we take a look at jQuery plug-ins, consider the code snippet below:


var userDefinedFunction = function ($) { 
  //your code here
};
userDefinedFunction (jQuery);

is equivalent to


(function ($) { 
    //your code here
})(jQuery)


Having discussed this, let me give you ahead up on writing customized plug-ins. Just follow the steps below and your customized JQuery plug-in is ready!

  1. Let us consider that we write a plugin by name sample.
  2. You have to have a file jquery.sample.js
  3. Within the file we can have methods for the plug-in. For this, you need to extend the JQuery object as follows:
  4. (function($){  
    $.fn.sample = function() {});
  5. You can now access this function using the below code in your html file:
  6. <script language="javascript" type="text/javascript">
                $('#<form name>').sample();
    </script>
    
  7. Default settings for this plug-in is created by using the code



    $.fn.sample = function(options) 
    {var defaults = {inputVal: 1, trueVal : 'true', falseVal : 'false',submitButton : '#submit'}
    var pluginOptions = $.extend(defaults, options);
    



  8. If ever you intend to call this function with new values which are different from the default values, use the following snippet and get it done!
  9. <script language="javascript" type="text/javascript">
                $('#<form name>').sample({inputVal:2, trueVal:'yes',falseVal:'no', submitButton:'#ClickMe');
    </script>
    
  10. As such, this was a nice learning and a great heads up on JQuery plug-ins!But, there are much more interesting tips to come! Stay tuned!



Dec 31, 2009

Learn JQuery Here! How to Invoke JQuery?

Of late, JQuery seem to have more features than mere javascripting for web development. Go through this post and get to know how to invoke a simple jquery from a html page.
  • For using jquery, forst and foremost, you need to download the JQuery API (which is just a .js file available at http://docs.jquery.com/Downloading_jQuery#Current_Release).
  • Place this file jquery-1.3.2.js (current release when this post is being written) into a folder "test".
  • Write out a sample html page as shown below and place it in the same folder 'test'
<html>
<head>
<script language="javascript" src="jquery-1.3.2.js"></script>
<script language="javascript" src="sample.js"></script>
Heading of the Sample HTML Page
</head>
<title>
Title of the sample HTML Page
</title>
<body>
<p>
This is a sample HTML Page which invokes a jquery before page loads!
</body>
</html>
  • As per the above code, you have included the JQuery API into the html file using the following code
<script language="javascript" src="jquery-1.3.2.js"></script><br />
  • Now, your browser would understand that you would be using jquery for the html that is being rendered.
  • Further to this, you can have a separate js file by name sample.js, where you can write all teh jquery functions that would be invoked when the html page is rendered. For the sample, lets consider that teh sample.js has the following lines. Place this file too in the 'test' folder and invoke sample.html by opening it in IE or mozilla.
<br />$(document).ready(function() {<br />alert('invoked jquery');<br />   <br /> });<br />
  • In the above case, the $(document).ready(function() is equivalent to javascripts body onLoad() function. So when the html is loaded, the jquery is invoked.<br>The ready() function of jquery gets executed and the message is alerted.
  • So, now you have understood how to invoke jquery from a very simple html page!! You are set to go ahead! All the best!






Dec 30, 2009

Adding Tabs in HTML

Being into Java development for a while, I had a chance to work with tabs in html. One of the easiest ways to add tabs to a html page and link to url that I came across is as given below in the following code. I thought this might be useful for some one some day and hence this post.

<ul id="tabs">
<li class="active"><a href="#" title="tab1">Title Tab 1</a></li>
<li><a href="#" title="tab2">Title Tab 2</a></li>
</ul>

In the above code, just change the href to the link as in your
application. You are done! You can have a UI with a couple of tabs and
it would look great!All the best! If you really found this post useful,
please drop your comment in the comment form displayed below! Thanks