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

> 2 Jquery Questions

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

 
Reply to this topicStart new topic
> 2 Jquery Questions
Jason
post Oct 28 2008, 05:49 AM
Post #1


Master of the Universe
Group Icon

Posts: 1,298
Joined: 15-February 08
From: London, England


1) How do I select a variable ID? The id is post_ID. ID being a number. So if someone clicks on post_1 or post_987, both will trigger this same event.

2) Can JQuery set and read cookies?


--------------------
Go to the top of the page
 
+Quote Post
MikeHopley
post Oct 28 2008, 06:08 AM
Post #2


Squeeze Machine
Group Icon

Posts: 682
Joined: 15-February 08
From: UK


1) Why not add a static class, and select by class instead?

2) Not natively. There is a cookie plugin, but I found it to be viciously buggy (see my comments). The problem is apparently caused by the script not setting a default path. Although this bug can probably be avoided if you take care, I just reverted to the Quirksmode script that works flawlessly out-the-box and actually has shorter syntax.

The author is an arrogant sod, who fails to appreciate that his plugin might be used by someone who doesn't understand the hidden dangers (someone like me). It completely screwed up my website until I realised what was going on; and the peculiar nature of this screw-up meant that tracking the bug was difficult.

No doubt the plug-in is technically flawless; but it still caused me problems, because it has a huge pitfall to avoid when integrating it with your site -- a pitfall with spikes at the bottom. I wasted a lot of time on this rubbish; you, however, may be savvy enough to dodge the pit. Caveat scriptor. wink.gif
Go to the top of the page
 
+Quote Post
japh
post Oct 28 2008, 06:11 AM
Post #3


Squeeze Machine
Group Icon

Posts: 508
Joined: 7-October 08
From: Australia


1) You could possibly do it like:
CODE
    $('div').each(function () {
        if ($(this).attr('id').indexOf('post_') > -1) {
            $(this).click(callbackFunction);
        }
    });


Not sure how efficient that would be... and maybe you don't have any idea what numbers

2) jQuery can (because Javascript can), there's a jQuery plugin called cookie that you could use to do it


--------------------
The more you visit, the more I'll post: http://japheththomson.com/
Go to the top of the page
 
+Quote Post
japh
post Oct 28 2008, 06:14 AM
Post #4


Squeeze Machine
Group Icon

Posts: 508
Joined: 7-October 08
From: Australia


QUOTE
Not natively.

Depends what you mean I guess, as Javascript can, therefore jQuery can...

Also, the cookie plugin you link to is the same one I link to, except that site is about 2 years out of date, so hopefully the one on the jQuery site is more recent and bug-free!

EDIT:
There's also a second jQuery plugin for cookies listed on the jQuery plugins repo: http://plugins.jquery.com/project/cookies


--------------------
The more you visit, the more I'll post: http://japheththomson.com/
Go to the top of the page
 
+Quote Post
MikeHopley
post Oct 28 2008, 06:23 AM
Post #5


Squeeze Machine
Group Icon

Posts: 682
Joined: 15-February 08
From: UK


QUOTE (japh @ Oct 28 2008, 11:14 AM) *
Depends what you mean I guess, as Javascript can, therefore jQuery can...


Sorry, I was assuming that Jason knew that Javascript can.

I meant that you can't just call some neat jQuery built-in function. You have to write the function yourself, or grab someone else's function (be it jQuery-specific or not).
Go to the top of the page
 
+Quote Post
Rakuli
post Oct 28 2008, 06:26 AM
Post #6


Squeeze Machine
Group Icon

Posts: 766
Joined: 13-February 08
From: Catching the squeezed drips downunder.


<smallrant>It becomes frustrating when a framework becomes so popular and it is treated as a language in itself. jQuery can do absolutely anything that javascript can because jQuery is not a language, it's a collection of functions that makes life a bit easier for the client side coder. I love jQuery but it is disappointing that everyone seems to want to do everything with jQuery now and instead of getting a little innovative with their code, they look for a plugin for jQuery. Javascript is more than jQuery</smallrant>

smile.gif ^ Sorry about that...


As for selecting based on a pattern, Japh was close but there is a slightly better way built into jQuery itself.

$('div[id^=post_]').click(stuff);

That will select all divs that have an id that starts with post_


--------------------
Luke Dingle . com

Turn Over a Playful Leaf on Web Design -- read about the javascript cat
Go to the top of the page
 
+Quote Post
japh
post Oct 28 2008, 06:34 AM
Post #7


Squeeze Machine
Group Icon

Posts: 508
Joined: 7-October 08
From: Australia


You can use regular expressions in your selector?? ohmy.gif Awesome!! *faints*


--------------------
The more you visit, the more I'll post: http://japheththomson.com/
Go to the top of the page
 
+Quote Post
MikeHopley
post Oct 28 2008, 06:56 AM
Post #8


Squeeze Machine
Group Icon

Posts: 682
Joined: 15-February 08
From: UK


QUOTE (Rakuli @ Oct 28 2008, 11:26 AM) *
$('div[id^=post_]').click(stuff);


Ah yes, I'd forgotten about that. It is rather brilliant, and I've used it before somewhere.

Related to this: I really recommend looking over the jQuery documentation -- it's pretty well organised, and you can find some surprising magic such as the above. smile.gif
QUOTE
I love jQuery but it is disappointing that everyone seems to want to do everything with jQuery now and instead of getting a little innovative with their code, they look for a plugin for jQuery. Javascript is more than jQuery


Well said!

I found myself doing exactly that with my initial, virginal jQuery infatuation. That's why I went looking for a cookie plug-in -- not because there was any real benefit, but because I wanted to replace everything with jQuery. I found a plug-in, replaced PPK's stalwart javascript with it, and then my site broke.

The irony was that PPK's syntax is actually shorter and easier than Karl's jQuery plug-in. So the only possible reason for my using Karl's script was that I had become obsessed with jQuery. Thankfully, although many people defend irrational obsessions by covering them in protective padding, I prefer to take a razor blade and gouge them out; after the bleeding has stopped, I no longer have something dirty growing on me. biggrin.gif I can't stand my mind becoming disordered.
Go to the top of the page
 
+Quote Post
Jason
post Oct 28 2008, 07:10 AM
Post #9


Master of the Universe
Group Icon

Posts: 1,298
Joined: 15-February 08
From: London, England


Cheers guys, I got it working and it works great.

Regarding the JQuery Cookie Plugin. I was looking for a quick and easy solution. I gave it a spin but it was silently failing with no errors or warnings. I couldn't work out why so I created a my own cookie functions. smile.gif


--------------------
Go to the top of the page
 
+Quote Post
japh
post Oct 28 2008, 07:15 AM
Post #10


Squeeze Machine
Group Icon

Posts: 508
Joined: 7-October 08
From: Australia


Nice work, Jason smile.gif

Glad you got it going!


--------------------
The more you visit, the more I'll post: http://japheththomson.com/
Go to the top of the page
 
+Quote Post
MikeHopley
post Oct 28 2008, 07:31 AM
Post #11


Squeeze Machine
Group Icon

Posts: 682
Joined: 15-February 08
From: UK


QUOTE
I gave it a spin but it was silently failing with no errors or warnings.


I had exactly the same experience, which is why I consider it a crumby piece of coding.
Go to the top of the page
 
+Quote Post
rich97
post Oct 28 2008, 08:38 AM
Post #12


Rapid Squeezer
Group Icon

Posts: 261
Joined: 27-August 08
From: London, UK


QUOTE (Rakuli @ Oct 28 2008, 12:26 PM) *
<smallrant>It becomes frustrating when a framework becomes so popular and it is treated as a language in itself. jQuery can do absolutely anything that javascript can because jQuery is not a language, it's a collection of functions that makes life a bit easier for the client side coder. I love jQuery but it is disappointing that everyone seems to want to do everything with jQuery now and instead of getting a little innovative with their code, they look for a plugin for jQuery. Javascript is more than jQuery</smallrant>


Why reinvent the wheel when the wheel is perfectly fine and distrubuted under an MIT licence? tongue.gif Would you code a whole text editor when you can just use TinyMCE? I have too much to fit into my working week anyway without having to redo stuff somone else has already done much better than I could of done anyway.

Besides, a carpenter dosn't cut down a tree and make it into 2x4 before using it, they just put the bits together. I do take a lot of stuff from the web community but I try and justify that by helping out on forums and IRC surley, that helps boost creativity and innovation.


--------------------
Go to the top of the page
 
+Quote Post
rich97
post Oct 28 2008, 08:39 AM
Post #13


Rapid Squeezer
Group Icon

Posts: 261
Joined: 27-August 08
From: London, UK


QUOTE (Jason @ Oct 28 2008, 01:10 PM) *
Cheers guys, I got it working and it works great.

Regarding the JQuery Cookie Plugin. I was looking for a quick and easy solution. I gave it a spin but it was silently failing with no errors or warnings. I couldn't work out why so I created a my own cookie functions. smile.gif


Could'nt you just use an ajax request?


--------------------
Go to the top of the page
 
+Quote Post
MikeHopley
post Oct 28 2008, 08:54 AM
Post #14


Squeeze Machine
Group Icon

Posts: 682
Joined: 15-February 08
From: UK


QUOTE (rich97 @ Oct 28 2008, 01:38 PM) *
I have too much to fit into my working week anyway without having to redo stuff somone else has already done much better than I could of done anyway.


I presume that wasn't Rakuli's point. It's certainly not what I intended.

jQuery is great, but so is other well-written javascript code. What's wrong with using PPK's cookie javascript? Why does it have to be jQuery?
Go to the top of the page
 
+Quote Post
rich97
post Oct 28 2008, 09:23 AM
Post #15


Rapid Squeezer
Group Icon

Posts: 261
Joined: 27-August 08
From: London, UK


QUOTE (MikeHopley @ Oct 28 2008, 01:54 PM) *
I presume that wasn't Rakuli's point. It's certainly not what I intended.

jQuery is great, but so is other well-written javascript code. What's wrong with using PPK's cookie javascript? Why does it have to be jQuery?


Ha, OK I missunderstood. I'm not adverse to using traditional JS but if I'm using jQuery on a project I'd rather find a plugin (I have coding OCD and it feels neater).


--------------------
Go to the top of the page
 
+Quote Post
Rakuli
post Oct 28 2008, 04:58 PM
Post #16


Squeeze Machine
Group Icon

Posts: 766
Joined: 13-February 08
From: Catching the squeezed drips downunder.


QUOTE
Why reinvent the wheel when the wheel is perfectly fine and distrubuted under an MIT licence? tongue.gif Would you code a whole text editor when you can just use TinyMCE? I have too much to fit into my working week anyway without having to redo stuff somone else has already done much better than I could of done anyway.

Besides, a carpenter dosn't cut down a tree and make it into 2x4 before using it, they just put the bits together. I do take a lot of stuff from the web community but I try and justify that by helping out on forums and IRC surley, that helps boost creativity and innovation.


Entirely not my point. If you have seen my articles on The Squeeze or even just my chat on the forum, it's apparent I am as big a jQuery fan as can be (well, perhaps not quite as big as Mike Yet biggrin.gif ). What I was saying though is that some people, present thread company excluded, will try something else if their idea does not have a jQuery plugin. I love the flexibility of using jQuery and have taken to writing my own plugins but if I get an idea that jQuery doesn't implement I will write it myself and not give up for lack of a plugin.

A carpenter won't cut down a tree to make a 2x4 but they won't build furniture out of flat-packs and not build something if Ikea doesn't have it in stock. My main point in this rant was to outlay my frustration that jQuery has become so big people think it is Javascript and not a brilliant tool built from it.


--------------------
Luke Dingle . com

Turn Over a Playful Leaf on Web Design -- read about the javascript cat
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:

 

Collapse

> Similar Topics

    Topic Title Replies Topic Starter Views Last Action
No New Posts   7 Jason 302 23rd February 2008 - 10:46 AM
Last post by: rewake
No New Posts   2 Jason 382 6th May 2008 - 07:33 AM
Last post by: Rakuli
No new   23 Itsumishi 647 16th May 2008 - 03:55 AM
Last post by: Rakuli
No New Posts   8 unitedcraig 575 9th September 2008 - 10:03 PM
Last post by: Monie
No new 26 Linda 352 1st June 2008 - 07:36 PM
Last post by: Linda