declare

Custom Functions: jQuery



Sharing buttons:

today we shall see functions in jQuery

you could write functions in two ways in

jQuery we shall see how it is if you are

familiar with some other languages like

C C++ Java or even PHP you would already

know the syntax of writing a

user-defined function or s custom

function well we have been writing this

function it is called as the anonymous

function usually we used to write

something here but for now for writing

custom functions we can write our

function definition outside this

document dot ready scope so function is

a keyboard a few N one is the function

name parenthesis inside the brackets we

write we select the span tag and append

function one well actually nothing

happens here because we have just

written our custom function definition

and we haven't used it anywhere still so

to use it we need to use its name and

the parentheses indicates that it's a

function the entire thing is a function

call so now we are using our function

using its name so instead of this we

shall see how to pass parameters to

functions I'll take a local variable

here as txt and assign a string to it as

it is a string I'll include it inside

double quotes and I will pass this txt

variable to our function to receive it

I'll take a local variable here it is

local to our function f u n1 and then

append it to span so this text which we

are writing here is pass it through our

if u n1 call function call which is sent

to the function definition which is then

appended to the span tag inside dock

loader this is what we passed to our

function instead we could also write it

directly inside our function call

reducing the number of line of code and

you could make it look cleaner see I

I'll pass passing parameters to function

this could pass this parameter to txt

local variable of fun1 function so it is

reflecting so it's working so now we

have we could write this function in

another way let's see how to write it so

if our function doesn't return any value

we could write return false to keep the

function flow the execution flow clear

so another type of function is I'll take

a variable called software is equal to

function it's a keyword inside that I'll

write my actions that is some statements

to execute before that I'll take a

button here with an ID of name so that

we could show some company name once the

user clicks on this button so we know

the code select the button with ID name

once the user clicks on it well we will

call this function that is software I

will remove this line

and write this inside our document dot

ready once the user clicks on the button

and select the span tag and append

something I'll append techno tip IT

solutions inside header one that is h1

tag but I need to call this so once the

user clicks on the button

I'll call so that our next line of text

displays in next line I have added BR

tag here so select the button the tidy

name once the user clicks usually we

used to write some anonymous function

inside this like function parentheses

and then inside bracket we would call if

if it is a function we would call fun1

here but in the second type of function

declaration and usage we'll just pass on

this variable name here and that's it we

are done open the vegetable browser once

I click I will get techno tip IT

solutions with a header of h1 well we

have seen another type binding that is

bind inside parentheses I'll write the

event which is click next the function

name which is software in our case we

know we need not include parentheses for

this type of function now I'll change

this to some other company

it's like Microsoft limited Microsoft

Limited I have commented the first line

let's see once I click Microsoft Limited

is printed so these are the two types of

functions one thing we have left or is

written type so to demonstrate that I

will take two local variables here once

the user passes these two variables that

is a and B value of a and B I'll add

them and store inside another local

variable C and then return the value of

C which is result now I will take a

variable for D let me pass two values 10

and 20 which will be passed to this

function and the result will be returned

and stored inside the variable D now

I'll print that value inside our span

tag that's it this is how return value

works return type was just 30 10 plus 20

is 30 this is how we return to reduce

the line of code we could also write

return inside parentheses just add the

value of a and B we could do this too so

this is how we write functions they have

shown two types of writing the function

and passing the parameters returning the

values and if when there is nothing to

return we need to write return false

this is the standard so please practice

this because we will be writing all our

application using functions because of

because functions help in reusability if

you are doing

the same tasks repeatedly in more than

one place write this code inside a

function and call that function this

reduces the number of lines of code and

helps in maintaining the clarity in our

code and simplifies the logic flow and a

lot more things we will learn it once

you start building the applications and

start using these functions so please

practice this please share this video on

Facebook Google+ Twitter Linkedin

etcetera subscribe to our blog techno

tip dot-com and to our YouTube channel

techno tip dot or G and please like this

video on YouTube