Archive for the ‘jQuery’ Category

A interesting problem came in today, a friend of mine requested to code a text area box such that when you press Enter Key inside it. It adds a new line and add comma to the previous line end.

Example:

Respected Customer (Now if i press enter here)

The above should look like: Respected Customer,

How to achieve this:

Logic Designing:

1. When enter key is press detect it

2. When detected get the whole string

3. Add the comma at the last

4. Now add this whole text to the textarea

Code:

<script>
 $(document).ready(
  function(){
   $('#txtarea').keypress(function(e) {
	if(e.keyCode == 13) {
	var string = $('#txtarea').val();
	string = string + ',';
	$('#txtarea').val(string);
   }
});
});
</script>

Note: #textarea is the Id of the textarea box.

Hope it helps.

You can demo the above version at: Demo

jQuery

Lets create a function inorder to achieve this effect.

function scrollTo(classname){
  $('html,body').animate({scrollTop:
     $("."+classname).offset().top},'slow');
}

By using this function you simply need to substitute the classname inorder to achieve the scrolling effect to a particular position.

 


		
jQuery

To get the text of the selected option:

$("#your_select :selected").text();

To get the value of the selected option:

$("#your_select").val();
jQuery

When a page is exiting or unloading, the ‘unload‘ event will be activate, however, this event can not stop a page from exit. You can bind the ‘beforeunload‘ event to $(windows) object to stop a page from exit , unload or navigating away.

<html>
<head>
<title>Refresh a page in jQuery</title>
<script type="text/javascript" 
src="https://ajax.googleapis.com/ajax/libs/jquery/
1.7.1/jquery.js"></script>
</head>

<body>
<h1>Stop a page from exit with jQuery</h1>
<button id="reload">Refresh a Page in
jQuery</button>
<script type="text/javascript">
	$('#reload').click(function() {
	 	location.reload();
	});
	$(window).bind('beforeunload', function(){
		return 'Refreshing this page will
remove all your setup information.';
	});
</script>
</body>
</html>
jQuery

Runloop is a jQuery plugin designed to support comprehensive animations running across many different elements.

Demo of the plugin is available here: Demo

jQuery Plugins

In JavaScript null is an object. There’s another value for things that don’t exist, undefined. The DOM returns null for almost all cases where it fails to find some structure in the document, but in JavaScript itself undefined is the value used.

Second, no, they are not directly equivalent. If you really want to check for null, do:

if (null == yourvar) // with casting
if (null === yourvar) // without casting

If you want to check if a variable exist

if (typeof yourvar != 'undefined') // Any scope
if (window['varname'] != undefined) // Global scope
if (window['varname'] != void 0) // Old browsers

If you know the variable exists but don’t know if there’s any value stored in it:

if (undefined != yourvar)
if (void 0 != yourvar) // for older browsers

If you want to know if a member exists independent of whether it has been assigned a value or not:

if ('membername' in object) // With inheritance
if (object.hasOwnProperty('membername')) // Without

If you want to to know whether a variable autocasts to true:

if(variablename)

I probably forgot some method as well…

jQuery

Let suppose you have an anchor and you want it to be click automatically as the page loads. Simply use the following:

Anchor between body tag:

<a class="info_link" href="#linkhere">Anchor</a>

And for jQuery script use:

$(function(){
  $('.info_link').click(function(){
    alert($(this).attr('href'));
  });
});
jQuery

One of the best shining stars of the online presentation world is Prezi – a totally different way of presenting your presentations online. Mimicking this functionality, impress.js is a javascript library created by developer Bartek Szopka, which takes the power of CSS3 transforms and transitions and enhances it – recreating the Prezi presentation experience in the browser. If you’ve a browser which supports the latest and greatest in CSS3 3D transforms – have a peek at the   Read More ...

jQuery Plugins