Showing posts with label Javascript. Show all posts
Showing posts with label Javascript. Show all posts

Tuesday, November 22, 2011

JQuery snippets

JQuery is the one of the awesome in javascrijpt. Here i list out some snippets

Disable the "Enter" key form submit


By default all HTML forms will submit when Enter key pressed. Sometime it will trouble. Using jquery we can easily disable the short cut.Here is the code we can easily prevent the form submit using enter key.

$("#form").keypress(function(e) {
  if (e.which == 13) {
    return false;
  }
});

Source : http://snipplr.com/view/10943/disable-enter-via-jquery/

Disable right click


Some time we want to disable the right click in a page, via JQuery it also easy one.Here is the code snippet for this one

$(function() {
$(this).bind("contextmenu", function(e) {
e.preventDefault();
});
});

Using the above one you can prevent the right click in your pages

Source : http://snipplr.com/view/34189/

Thursday, July 21, 2011

Add new font family to your site

Style take important role in website, some time our customer asked some different font for site.
Google give lots of fonts to style our site we can use this via some simple steps with out no cost.

For more detail about Google Font please see the Google web Fonts. Here there are some simple click we can get our required font style and get the link source for our font and use that style.


Keyword: New fond type, New font style , Font family, how to insert google font

Thursday, July 14, 2011

Get elements from html string.

some time we have a html content as a string need to get the elements from string, i meet the same problem in my project and searching a while in google and finally i decided to create a dummy div and insert the html string in that div element using

document.getElementById('div').innerHtml = htmlstring;

from that div i can get all html elements as object.

div.getElementsByName("*");

using this I got  the elements a array and while we can get each object.

Tuesday, July 12, 2011

Get all elements in div using javascript

In javascript wee can get all html elements using getelemensbyname. Here is the sample for that

div = document.getElementsById('sample');

Elements = div.getElementsByName("*");

This will return all html elements with in a div.

Wednesday, June 29, 2011

How to create instants for dynamic class name in js?

In js we can create a instant for class just like new classname(). If the you going to call a dynamic class name mean we meet a problem

(ex) if you going to call two diff class name based on selected object

name = 'test'
new 'class'+name+'()';

JS will show u on error, we  can overcome with eval() function. just use

eval ('calss'+name+'()');

also you can pass arguments like this

eval ('calss'+name+'(arg1,arg2)');

Tuesday, June 14, 2011

work with select box in javascript

Here we going to see about how to work with select box in javascript. first we going to see how to check any options selected in select box. we can easily check using selectedIndex.

document.getElementById('id').selectedIndex;

it will return '-1' when no one selected otherwise give the index of selected option like 0,1,2 ...

find number of options in select  box:
--------------------------------------
 just check the length , it will return number of options

document.getElementById('id').option.length;

Add options to select box:
---------------------------
Adding options to select box via options.add() like below
options.text = 'test';
options.value = 'test_value';
document.getElementById('id').options.add(options);

Remove option from select box:
---------------------------------
similar to add option you can remove the options from select box using options.remove();
In remove option we need to pass the index value
document.getElementById('id').options.remove(0);
This will remove the first option in select box. we will remove all options using for loop or just set the options length as 0.

document.getElementById('id').option.length=0;

this will remove all the options in select box.


Monday, May 23, 2011

How get file type using javascript

This is post about how to get the filetype from file name. nothing special just using split() and pop() function

<script>
filename = 'test.php';
alert(filename.split(.).pop());
</script>

alert will return php, because the split function split the filename and return a array as [0]=>'test', [1]=>php.

pop() will pop the last value from array.

Sunday, November 14, 2010

Different ways to define javascript object

Creating objects using  new Object()


    var student = new Object();
    student.name           = 'John';
    student.rollNumber  = '12';

Creating objects using Literal Notation
     var student = {
                           name : "john",
                           rollNumber : "12"
                          }