tags:

views:

46

answers:

2

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>
....
A: 

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

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

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();
} 
Mario Menger
it means `div`-s id must be like `1,2,3...?`
nectar
no, `div`-s ids must be like `info1,info2,info3` - I've updated my answer to make this clearer
Mario Menger
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.
nectar