Welcome Guest!

If you do not have an account yet on The Web Squeeze forums, please Register! It’s FREE and there are many benefits:

  • Receive Fast Advice
  • Learn Programming Languages
  • Get Professional Website Reviews
  • Quick Troubleshooting Assistance

> Jquery Problem

This is a discussion on Jquery Problem, within the Javascript section. This forum and the thread "Jquery Problem" are both part of the Frameworks category.

 
Reply to this topicStart new topic
> Jquery Problem
jackfranklin
post Jun 21 2008, 12:02 PM
Post #1


Rapid Squeezer
****

Group: Members
Posts: 100
Joined: 16-February 08
Member No.: 163



This sliding thing is not working. What am I doing wrong?

CODE
  <script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){$("#divScroll").click(function(e) { $("#scrollDiv").slideToggle("slow"); e.preventDefault()}})



</script>

HTML
CODE
<a href="#" id="divScroll">Show the Div!</a>
<div id="scrollDiv">
<p>Some text in the div</p>
</div>


This post has been edited by jackfranklin: Jun 21 2008, 12:04 PM


--------------------
Jack Franklin | Eportfolio & Weblog
Go to the top of the page
 
+Quote Post
jackfranklin
post Jun 21 2008, 12:14 PM
Post #2


Rapid Squeezer
****

Group: Members
Posts: 100
Joined: 16-February 08
Member No.: 163



*Sigh* I knew it would be a stupid mistake! Working code:
CODE
$(document).ready(function(){
$("#divScroll").click(function(e){
$("#scrollDiv").slideToggle("slow"); e.preventDefault();
});
});

Missed out a bracket and semi-colon.


--------------------
Jack Franklin | Eportfolio & Weblog
Go to the top of the page
 
+Quote Post
MikeHopley
post Jun 21 2008, 12:27 PM
Post #3


Squeeze Machine
*****

Group: Mentor
Posts: 505
Joined: 15-February 08
From: UK
Member No.: 143



Don't forget you can use Firefox to check for javascript errors. smile.gif

Just errors, mind you -- don't check for warnings, because jquery.js has hairy code that will throw warnings by the bucketload. But then, so does Google Analytics.
Go to the top of the page
 
+Quote Post
jackfranklin
post Jun 21 2008, 12:35 PM
Post #4


Rapid Squeezer
****

Group: Members
Posts: 100
Joined: 16-February 08
Member No.: 163



How do I do that? Just via the Source?


--------------------
Jack Franklin | Eportfolio & Weblog
Go to the top of the page
 
+Quote Post
Rakuli
post Jun 21 2008, 12:37 PM
Post #5


Squeeze Machine
*****

Group: Team Leaders
Posts: 568
Joined: 13-February 08
From: Catching the squeezed drips downunder.
Member No.: 13



QUOTE
How do I do that? Just via the Source?


Tools -> Error Console

Or, you can download the FireBug plugin. You have to be careful with the latter because it will not always pick up onload errors, you have to get them from the Firefox error console. Firebug is worth the download for its myriad of other features though.


--------------------
Bright Idea? -- Don't Let it disappear
Go to the top of the page
 
+Quote Post
jackfranklin
post Jun 21 2008, 12:41 PM
Post #6


Rapid Squeezer
****

Group: Members
Posts: 100
Joined: 16-February 08
Member No.: 163



Cool cheers. BTW, I know there is click(function... but is there a one for hover? like hover(function...)?


--------------------
Jack Franklin | Eportfolio & Weblog
Go to the top of the page
 
+Quote Post
Rakuli
post Jun 21 2008, 01:17 PM
Post #7


Squeeze Machine
*****

Group: Team Leaders
Posts: 568
Joined: 13-February 08
From: Catching the squeezed drips downunder.
Member No.: 13



yes $(element).mouseover()

:hover is a css pseudo class, onmouseover is the javascript equal (use $(element).mouseout() to turn any effect off)


--------------------
Bright Idea? -- Don't Let it disappear
Go to the top of the page
 
+Quote Post
MikeHopley
post Jun 21 2008, 01:45 PM
Post #8


Squeeze Machine
*****

Group: Mentor
Posts: 505
Joined: 15-February 08
From: UK
Member No.: 143



QUOTE (Rakuli @ Jun 21 2008, 07:17 PM) *
yes $(element).mouseover()

:hover is a css pseudo class, onmouseover is the javascript equal (use $(element).mouseout() to turn any effect off)


Actually, there's a hover() event too.

Why write two lines when you can write one? *pets jQuery* biggrin.gif
Go to the top of the page
 
+Quote Post
Rakuli
post Jun 21 2008, 02:09 PM
Post #9


Squeeze Machine
*****

Group: Team Leaders
Posts: 568
Joined: 13-February 08
From: Catching the squeezed drips downunder.
Member No.: 13



LOL. Well, you learn something jQuery new everyday. ** Imagines Mike filing adoption papers to take jQuery and show off the 'talented boy' at family functions **


--------------------
Bright Idea? -- Don't Let it disappear
Go to the top of the page
 
+Quote Post
jackfranklin
post Jun 21 2008, 02:24 PM
Post #10


Rapid Squeezer
****

Group: Members
Posts: 100
Joined: 16-February 08
Member No.: 163



Just a question, why for the Hover are they using:
CODE
  $(this).append($("<span> ***</span>"));

That extra $ after the append? Surely they could just use:
CODE
append("<span> ***</span>");


And what does this bit do?
CODE
find("span:last")


I get that it finds the span and then gets rid of it, but what is the :last all about?


--------------------
Jack Franklin | Eportfolio & Weblog
Go to the top of the page
 
+Quote Post
Rakuli
post Jun 21 2008, 02:41 PM
Post #11


Squeeze Machine
*****

Group: Team Leaders
Posts: 568
Joined: 13-February 08
From: Catching the squeezed drips downunder.
Member No.: 13



They are using that option so that there is a DOM Node created before being apended to the element being hovered over. so

$(this).append ( /*create new node */ $('<span> ***</span>') );

The last part finds the last <span> tag inside the element (just in case that element already has a span in it.


--------------------
Bright Idea? -- Don't Let it disappear
Go to the top of the page
 
+Quote Post
jackfranklin
post Jun 21 2008, 03:00 PM
Post #12


Rapid Squeezer
****

Group: Members
Posts: 100
Joined: 16-February 08
Member No.: 163



Ok, excuse my newbieness, but what advantage does the node have, and if I removed the $ and have:
append("<span> ***</span>");

Would it work?


--------------------
Jack Franklin | Eportfolio & Weblog
Go to the top of the page
 
+Quote Post
Rakuli
post Jun 21 2008, 03:11 PM
Post #13


Squeeze Machine
*****

Group: Team Leaders
Posts: 568
Joined: 13-February 08
From: Catching the squeezed drips downunder.
Member No.: 13



Yes it would work but if you take a look at the other jQuery thread (the one Mike started) you will see that there is a difference between appending a text string and appending a DOM node. It just comes down to complying or otherwise with the W3's reccomendations.

So if you want to append something, you should try to append a document node (which $("<span> ***</span>") creates ) rather than just append the text string which is sometimes added as element.innerHTML which is not a DOM property.

It's semantics really but best practices are that for a reason wink.gif


--------------------
Bright Idea? -- Don't Let it disappear
Go to the top of the page
 
+Quote Post
jackfranklin
post Jun 21 2008, 03:14 PM
Post #14


Rapid Squeezer
****

Group: Members
Posts: 100
Joined: 16-February 08
Member No.: 163



Ok thanks biggrin.gif

BTW = if you check any post with comments on my blog, check out the effect biggrin.gif

6 Lines of JQuery...


--------------------
Jack Franklin | Eportfolio & Weblog
Go to the top of the page
 
+Quote Post
Rakuli
post Jun 21 2008, 03:22 PM
Post #15


Squeeze Machine
*****

Group: Team Leaders
Posts: 568
Joined: 13-February 08
From: Catching the squeezed drips downunder.
Member No.: 13



6 lines? A bit bloated isn't it? LOL

I would just use $('#element').slideToggle('fast');, which I assume you are already using.

Also, don't forget to prevent the default behaviour for the element (in your case a link) so you don't get taken to the top or bottom of the page when you click a link

CODE
$(document).ready(function(){
  
  $('#thisLink').click(function (event) { event.preventDefault(); $('#element').slideToggle('fast'); });
});


--------------------
Bright Idea? -- Don't Let it disappear
Go to the top of the page
 
+Quote Post
jackfranklin
post Jun 21 2008, 03:23 PM
Post #16


Rapid Squeezer
****

Group: Members
Posts: 100
Joined: 16-February 08
Member No.: 163



I'm using slideToggle, but I had it on Slow. And I forgot the Preventing Default. Uploading now.


--------------------
Jack Franklin | Eportfolio & Weblog
Go to the top of the page
 
+Quote Post
MikeHopley
post Jun 21 2008, 03:32 PM
Post #17


Squeeze Machine
*****

Group: Mentor
Posts: 505
Joined: 15-February 08
From: UK
Member No.: 143



QUOTE (Rakuli @ Jun 21 2008, 09:11 PM) *
So if you want to append something, you should try to append a document node (which $("<span> ***</span>") creates ) rather than just append the text string which is sometimes added as element.innerHTML which is not a DOM property.


Hold on. Does that mean that, in general, I should use .append($(...)) rather than .append(...), assuming that I want the DOM and not innerHTML?
Go to the top of the page
 
+Quote Post
Rakuli
post Jun 21 2008, 03:43 PM
Post #18


Squeeze Machine
*****

Group: Team Leaders
Posts: 568
Joined: 13-February 08
From: Catching the squeezed drips downunder.
Member No.: 13



QUOTE
Hold on. Does that mean that, in general, I should use .append($(...)) rather than .append(...), assuming that I want the DOM and not innerHTML?


From reading the docs this was my understanding however after the discussion with you in the other jQuery thread and further examination of the source, it appears jQuery attemps to create DOM nodes regardless of which method is used.

If the append($(..)) method is used, a jQuery object of the created DOM nodes is created before it is appended -- assumably to increase speed if this element is referenced later in the script.

Using straight up append('..') does not change the behavior whereby the string is converted to DOM nodes first. I think some things may have changed since the docs were written or they haven't worded them as clearly so as to eliminate the confusion.

Their regex is pretty impressive but I'm sure there's quite a few occasions where it doesn't match which is why they have to have the innerHTML fallback.


--------------------
Bright Idea? -- Don't Let it disappear
Go to the top of the page
 
+Quote Post
MikeHopley
post Jun 21 2008, 05:56 PM
Post #19


Squeeze Machine
*****

Group: Mentor
Posts: 505
Joined: 15-February 08
From: UK
Member No.: 143



Ah. Thanks for that -- a good explanation. smile.gif

Now I just have to work out why IE is choking on my jQuery. Perhaps it's not quite as cross-browser as it claims.
Go to the top of the page
 
+Quote Post
karinne
post Jun 21 2008, 07:39 PM
Post #20


Co-Founder
******

Group: Co-Founders
Posts: 2,498
Joined: 13-February 08
From: Squeezin'
Member No.: 2



Moved to Frameworks > Javascript


--------------------
The Squeeze Store is now OPEN! Come on in and grab something!

a web design portfolio | web non-sense
I'm also on: del.icio.us | flickr | virb | facebook | twitter
The Web Squeeze is also on: virb | facebook | stumbleupon - JOIN IN!
Go to the top of the page
 
+Quote Post
If you found The Web Squeeze to be helpful, please donate so we can keep this site FREE, FRESH, and fortified with Web Design & Development info!
Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members: