A Flash Developer Resource Site

Results 1 to 14 of 14

Thread: [RESOLVED] Using jQuery with JavaScript function

  1. #1
    Moonlight shadow asheep_uk's Avatar
    Join Date
    Dec 2001
    Location
    London
    Posts
    2,010

    resolved [RESOLVED] Using jQuery with JavaScript function

    Yo, 'sup?

    Code:
    function minimise(minid)
    {
    	$(minid.' > div.body').slideUp("fast");
    }
    That code doesn't work.

    Anybody know what I'm suppose to write for the minid.' > div.body' bit?

  2. #2
    Bearded (M|G)od MyFriendIsATaco's Avatar
    Join Date
    Dec 2002
    Location
    Awesomeville.
    Posts
    3,045
    Is minid supposed to be an id? If so, you're missing the #.

    Code:
    $('#'+minid+' > div.body').slideUp('fast');

  3. #3
    Moonlight shadow asheep_uk's Avatar
    Join Date
    Dec 2001
    Location
    London
    Posts
    2,010
    Oh that's annoyingly obvious, but doesn't work...

    Just tried the code without the variable to make sure it is working, it does, so that's fine. I'm playing around with defining variables now. Any other ideas?

  4. #4
    Moonlight shadow asheep_uk's Avatar
    Join Date
    Dec 2001
    Location
    London
    Posts
    2,010
    Turns out my the other similar code isn't working in Firefox at all...
    Code:
    function closewidget(closeid)
    {
    	$(closeid).fadeOut('slow');
    }
    Firefox says "Error: blog is not defined" (blog is the closeid as an example)

    Works in Safari and Internet Explorer (never thought I'd write that).

  5. #5
    Moonlight shadow asheep_uk's Avatar
    Join Date
    Dec 2001
    Location
    London
    Posts
    2,010
    Update: been searching around for the Firefox and it seems like the entire web development world, who handles JavaScript is pretty pissed off with Mozilla. Haven't found a solution that keeps jQuery happy yet though...

  6. #6
    Bearded (M|G)od MyFriendIsATaco's Avatar
    Join Date
    Dec 2002
    Location
    Awesomeville.
    Posts
    3,045
    jQuery needs the # in the selector. So I don't know what to tell you. I've done this time and time again and it works just fine in all browsers.
    Code:
    var closeid='awesome';
    $('#'+closeid).fadeOut('slow');
    If you have an element with an id of "awesome" on the page, it will fade out.

  7. #7
    Moonlight shadow asheep_uk's Avatar
    Join Date
    Dec 2001
    Location
    London
    Posts
    2,010
    See that's what I thought, but this code works!
    Code:
    function minimise(minid)
    {
    	$(minid).slideToggle('fast');
    }

  8. #8
    Bearded (M|G)od MyFriendIsATaco's Avatar
    Join Date
    Dec 2002
    Location
    Awesomeville.
    Posts
    3,045
    Is minid a string? And what exactly is it's value? Also, is this online anywhere so I can check out your DOM?

  9. #9
    Moonlight shadow asheep_uk's Avatar
    Join Date
    Dec 2001
    Location
    London
    Posts
    2,010
    It's on a private server, it's got NDAs attached to it!

    minid is the id of the div.

    Code:
    <div class="widget" id="blog">
    	<div class="title">
    		<div class="close" onclick="closewidget(blog)"></div>
    		<div class="minimise" onclick="minimise(blog)"></div>
    		<h3><a href="/blog/">Blog</a></h3>
    	</div>
    	<div class="body">
    		<!-- body to slideup -->
    	</div>
    </div>
    So in theory the JavaScript and jQuery should be

    Code:
    function minimise(minid)
    {
    	$('#'+minid+' > div.body').slideUp("fast");
    }
    Which should keep the 'title', with the buttons, but close the 'body' of the 'widget'.

    Thanks for the help so far, it's a real puzzler!
    Last edited by asheep_uk; 10-27-2009 at 11:48 AM.

  10. #10
    Bearded (M|G)od MyFriendIsATaco's Avatar
    Join Date
    Dec 2002
    Location
    Awesomeville.
    Posts
    3,045
    Your problem is here: minimise(blog)

    'blog' is not a string. That's probably why you're getting mixed and unexpected results.

    You should use:
    Code:
    <div class="minimise" onclick="minimise('blog')"></div>
    See the difference? Now 'blog' is a string.

  11. #11
    Moonlight shadow asheep_uk's Avatar
    Join Date
    Dec 2001
    Location
    London
    Posts
    2,010
    Oh what a douche... Yeah, it works! You're a genius!

    Still have the Firefox problem though – both work in Safari and Internet Explorer, but Firefox is still whinging about it not being defined.

  12. #12
    Moonlight shadow asheep_uk's Avatar
    Join Date
    Dec 2001
    Location
    London
    Posts
    2,010
    Sorted out Firefox, it just needed a little attention around some other functions and it was happy!

    Thanks Taco!

  13. #13
    Bearded (M|G)od MyFriendIsATaco's Avatar
    Join Date
    Dec 2002
    Location
    Awesomeville.
    Posts
    3,045
    Well, to many help you out without seeing the problem, you're using jQuery, but not using it to it's fullest. The onclick attribute on an element shouldn't be used when binding click events. jQuery has support for that built in.
    Code:
    $(document).ready(function(e)
    {
        $('#body .title .close').click(function(e)
        {
            $('#body > div.body').slideUp('fast');
        });
    });
    You should be doing something like that.

  14. #14
    Moonlight shadow asheep_uk's Avatar
    Join Date
    Dec 2001
    Location
    London
    Posts
    2,010
    I didn't know it could do that! First proper jQuery project I've worked on, so a little slow! Damn useful, thank you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center