Hi, I recently started learning JQuery, and I have just one question, how would one make a function with a variable?
In JavaScript I would do it like so:
[code]
function coolfunction( somevar ){
document.write( somevar );
}
[/code]
However, this does apparently not work the same in JQuery, anyone know how to do this?
What do you mean it doesn't work the same in jQuery? All jQuery does is add more functionality to JavaScript.
That above code is JavaScript and therefore should still work.
As far as I know jQuery does not modify existing functionality.
Let me explain my problem further.
I have a div, which I want to show when I click a certain link.
I now have to code separately for every link.
I'd much rather have it with a function.
Right now I'm doing it like this:
[code]
$("#aboutlink").click(function() {
if( $("#about").is(":hidden")){
$("#about").show(2000);
} else {
$("#about").hide(1000);
}
} );
[/code]
I tried this:
[code]
function testfunc(divid) {
if( $(divid).is(":hidden")){
$(divid).show(2000);
} else {
$(divid).hide(1000);
}
} );
[/code]
But that's not working.
How would I go about doing this?
Edit:
Going to bed now, see you all in like 10 hours :P
What's calling testfunc and what's being passed as an argument?
The argument is probably "this" and he's probably using an onclick. :v:
How about...
[code]$("#aboutlink").click(
function() {
$("#about").toggle(1500);
}
);[/code]
Or is it important that the hide is quicker than the show, in which case...
[code]$("#aboutlink").click(
function() {
$("#about:hidden").show(2000);
$("#about:visible").hide(1000);
}
);[/code]
If you wanted a function to do something similar it'd be...
[code]function testfunc( divid ) {
$('#'+divid+':hidden').show(2000);
$('#'+divid+':visible').hide(1000);
}[/code]
Thanks DEADBEEF, trying that now.
Edit:
Should I be able to do this the same way as normal JS with an OnClick?
Say, I have a link, and I want it to show the div "testdiv" when I click on it?
[QUOTE=Hufterkruk;18485107]Should I be able to do this the same way as normal JS with an OnClick?
Say, I have a link, and I want it to show the div "testdiv" when I click on it?[/QUOTE]
Yeah, you should be able to do that.
After all, jQuery only extends the functionality of regular JavaScript.
It's still not working, but I'm going to bed now, I'll ask someone on school monday, he knows JS and JQuery (and a fuckton of other languages) really good.
[QUOTE=Hufterkruk;18495465]It's still not working, but I'm going to bed now, I'll ask someone on school monday, he knows JS and JQuery (and a fuckton of other languages) really good.[/QUOTE]
Might it be [url=http://www.facepunch.com/showpost.php?p=18493442&postcount=10]Youshedo[/url]?
On a serious note, are you by any chance defining things like display:none in a stylesheet instead of inline?
[QUOTE=a2h;18502718]Might it be [url=http://www.facepunch.com/showpost.php?p=18493442&postcount=10]Youshedo[/url]?
On a serious note, are you by any chance defining things like display:none in a stylesheet instead of inline?[/QUOTE]
Not defining anything as inline, I am indeed defining it as "display: none", but this is not a problem with the other divs that I'm trying to show, I only don't feel like coding the exact same thing with a name changed every time, which is why I want a function.
Sorry, you need to Log In to post a reply to this thread.