views:

74

answers:

4

I want to know that when we have a function with parameters that

works like a VB procedures

When and how can I use this function ?

How can we call this ?

<script type="text/javascript">

function place(div, image_x, image_y )
{

    $("#"+div).css("position", "absolute") ;

    $("#"+div).css("left",image_x+"px");

    $("#"+div).css("top",image_y+"px"); 

}

When and how can I use this function ?

I tried to use this in several ways , but It seems that JQuery didn't activate .

some of my efforts to call this : (maybe funny)

(function($) {
    // $(document).ready( function(){

    $.fn.Place = function place(div, image_x, image_y) {
    }) (jQuery)
jQuery(function($) {
    place("pnlLinx", 700 , 500 ) ;
});

or simply :

place("pnlLinx", 700 , 85 ) ;

with no effect

thanks in advance

This should be a simple question for people knew the structure of JQuery I'm not totally familiar with the structure of JQuery but I used it different ways in my codes .

this example is just for learning one part of this structure , that I had problem .

+4  A: 

I didn't completely get your question but here's what I can say,

$.fn.place = function (image_x, image_y) {
    this.css({
        position: 'absolute',
        left: image_x + 'px',
        top: image_y + 'px'
    });
};

Now you can do

$('#pnlLinx').place(700, 500);

Adding a function to the $.fn scope creates a jQuery method, that you can invoke on a jQuery object. And inside the method, this refers to the jQuery object the method is invoked on.

Edit: Is this what you need...

$( function ($) {

    function place(divId, image_x, image_y) {
        $('#' + divId).css({
            position: 'absolute',
            left: image_x + 'px',
            top: image_y + 'px'
        });
    }

    place('pnlLinx', 700, 500);

});

Edit 2: If you want the function under $,

$( function ($) {

    $.place = function (divId, image_x, image_y) {
        // ...
    }

    $.place('pnlLinx', 700, 500);

});

Good luck.

Shrikant Sharat
because I'm trying to learn the structure during my work your post anyway will help me , but I don't want to use this , I passed the name of the div to the function and all the processes will go inside of our function .The thing that I want to know more is how can we use a function not use regular events of JQuery or maybe just call it inside the ready() event of JQuery . ( calling of our defined function )
Sypress
I added another method, see if that is what you need...
Shrikant Sharat
the exception is I want to use the function outside of it ,place('pnlLinx', 700, 500);can I use it inside of ready part : $(ready().function()
Sypress
yeah, see the code in **Edit 2** :)
Shrikant Sharat
really thanks , you helped me alot .
Sypress
+1  A: 

You left off using jQuery as a parameter to the anonymous function you're writing, and you're adding Place as a method then trying to use it as a static function.

(function($) {
    // $(document).ready( function(){

    $.fn.Place = function place(div, image_x, image_y) {
    }) (jQuery)
jQuery(function($) {
    place("pnlLinx", 700 , 500 ) ;
});

should be

(function($) {

  $.place = function place(div, image_x, image_y) {

    $("#"+div).css("position", "absolute") ;
    $("#"+div).css("left",image_x+"px");
    $("#"+div).css("top",image_y+"px");
  };

})(jQuery);

Then you can

$.place('mydiv', 700, 500);

Note, I used lowercase P, as this is the convention for method names in JavaScript.

sidewaysmilk
Where to put this : $.place('mydiv', 700, 500);could it be inside the ready status of JQuery in my code ?
Sypress
you need to include function anywhere after you added jQuery library. After your function is included then you can call it like $.place('mydiv', 700, 500); in $(document).ready() or anywhere else after $.place function.
Ayaz Alavi
thanks sidewaysmilk ,good examples
Sypress
+2  A: 

Make sure jQuery library must included before any jquery code.

yogs
+4  A: 

Hi, See this http://www.jsfiddle.net/ , that might help you in understand where to put that code. Also Code in Javascript section of jsfiddle link needs to wrapped in <script> tag

Complete Code

$.fn.place = function (image_x, image_y) {
    this.css({
        position: 'absolute',
        left: image_x + 'px',
        top: image_y + 'px'
    });
};

function place(divId, image_x, image_y) {
   $('#' + divId).css({
        position: 'absolute',
        left: image_x + 'px',
        top: image_y + 'px'
   });
}


$(document).ready(function(){

    $('#pnlLinx').place(50, 100);


    place('pnlLinx', 100, 150);


    $('#pnlLinx').animate({color:"#dddddd"}, 500, function(){
            $(this).place(50, 100);      
            $(this).animate({color:"#aaaaaa"}, 1000, function(){
                      place($(this).attr("id"), 100, 150);
            });
    });

});
Ayaz Alavi
really helpful , thanks
Sypress
no problem bro :)
Ayaz Alavi