3

how to create own function and call it another function? like-

$('.mycls li').hover(function() {    
var divid = $(this).find('li').attr('some_attr');
call myfunc(divid);
});

function myfunc(divid)
{
$(divid).show();
//I want to hide rest all divs 
$('div#info1').hide();
$('div#info2').hide();
$('div#info3').hide();
}

I have 2 questions one is how to implement this logic in jquery second is which attribute can be used to reference the specific li to specific div

my divs are as-

<div id="info1">
//some information
</div>
<div id="info2">
</div>
....
2
  • 1
    You have already answered your first question. As for the second... I don't understand, please clarify. I think you have to show more of your HTML markup. Commented Jun 23, 2010 at 8:04
  • @Kling I just want to call to myfunc() on mouseover of li items. Commented Jun 23, 2010 at 8:13

3 Answers 3

5

Your functions should be able to operate on the wrapped set like other methods/functions of jquery. Consider a plugin, it is easy:

jQuery Plugin Tutorial

Or see:

Defining your own functions in jQuery

Sign up to request clarification or add additional context in comments.

1 Comment

have a look on my code is it correct- <script> $('.smenu li').mouseover( function(){ myfunnc(); }); jQuery.fn.myfunc = function() { $('div#telcosinfo').show("medium"); } </script>
2

I wrote this:

jQuery.fn.doSomeStuff = function(options, callback) {
    var $elem = $(this[0]);
    var args = options || {};
    
    // doSomeStuff
    alert("DOM element: "+$elem.html());
    alert("this ia a parameter:"+ args.aParameter);
    
    if (typeof callback == 'function') { // make sure the callback is a function
        callback.call(this); // brings the scope to the callback
    }
    return $elem; //ensures jquery chainability
};
  1. define your own function
  2. specify parameters and callback
  3. ensure jquery chainability

You can try it here

Comments

1

There is no keyword call that you need to use to call a function. So just using

myfunc(divid);

to call your function will do.

To link your li-s to your div-s you can use an id naming scheme, for instance, give all your li-s ids beginning with a d and all your div ids beginning with "info" as you have them, but have the bits after that be the same.

<li id='d1'> </li>
<li id='d2'> </li>
<li id='d3'> </li>

<div id='info1'></div>
<div id='info2'></div>
<div id='info3'></div>

and then use

$('.mycls li').hover(function() {      
    // get the number part of the li id and use it to
    // build the divid by appending it to "info"
    var divid = "info" + $(this).attr('id').slice(1);
    myfunc(divid);   
});   

function myfunc(divid)   
{
     $("#" + divid).show();   
     //hide all divs that do not have id=divid
     $("div:not(#" + divid + ")").hide();
} 

3 Comments

it means div-s id must be like 1,2,3...?
no, div-s ids must be like info1,info2,info3 - I've updated my answer to make this clearer
ya I got it...but that is not working even when I am passing var divid="info1"; myfunc(divid); it is not calling the function but all div-s of the page are getting disappeared.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.