views:

931

answers:

4

How can i call a jQuery function from javascript?

 //jquery
 $(function() {

        function my_fun(){
               /.. some operations ../
        } 
 });

 //just js
 function js_fun () {

       my_fun(); //== call jquery function 
 }
+2  A: 

You can't do it.

function(){

    function my_fun(){
           /.. some operations ../
    }
}

this is a closure. my_fun() is defined only inside Anonimus function. You can solve it by declaring my_fun() globally.

since $(function(){...}) just executes anonimous function when dom is ready there is no need to place my_fun() declaring inside. Or is there reason to declare function when dom is ready?

Of cause if you want to run this function ondomready you should do this:

function my_fun(){
       /.. some operations ../
}

$(function(){

    my_fun(); //run my_fun() ondomready

});

//=========== just js

function js_fun(){

   my_fun(); //== call my_fun() again
}
Eldar Djafarov
could you say me how to declare global function in jquery?If you have some examples it would be wonderful.
Actually i have showed you an example. jQuery is a part of Javascript. In Javascript we define global functions like in my example(fist one function is declared globally).
Eldar Djafarov
A: 

jQuery functions are called just like JavaScript functions.

For example, to dynamically add the class "red" to the document element with the id "orderedlist" using the jQuery addClass function:

$("#orderedlist").addClass("red");

As opposed to a regular line of JavaScript calling a regular function:

var x = document.getElementById("orderedlist");

addClass() is a jQuery function, getElementById() is a JavaScript function.

The dollar sign function makes the jQuery addClass function available.

The only difference is the jQuery example is calling the addclass function of the jQuery object $("#orderedlist") and the regular example is calling a function of the document object.

In your code

$(function() {
// code to execute when the DOM is ready
});

Is used to specify code to run when the DOM is ready.

It does not differentiate (as you may think) what is "jQuery code" from regular JavaScript code.

So, to answer your question, just call functions you defined as you normally would.

//create a function
function my_fun(){
  // call a jQuery function:
  $("#orderedlist").addClass("red");
}

//call the function you defined:
myfun();
Chris Tek
A: 

Tnx for help, it was helpful information. This is my problem. I look at it from wrong angle.

function new_line(){

var html= '<div><br><input type="text" value="" id="dateP_'+ i +'"></div>';

document.getElementById("container").innerHTML += html;

jQuery('#dateP_'+i).datepicker({showOn: 'button', buttonImage: 'calendar.gif', buttonImageOnly: true});
 i++;

}

The thing that i must do this with calling JS function. It's working only for the last added element.

A: 

I made it...

I just write

 jQuery('#container').append(html)

instead

 document.getElementById('container').innerHTML += html;