Project Perfect Mod Forums
:: Home :: Get Hosted :: PPM FAQ :: Forum FAQ :: Privacy Policy :: Search :: Memberlist :: Usergroups :: Register :: Profile :: Log in to check your private messages :: Log in ::


The time now is Fri Mar 29, 2024 8:17 am
All times are UTC + 0
Anyone good with javascript here?
Moderators: Global Moderators, Offtopic Moderators
Post new topic   Reply to topic Page 1 of 1 [10 Posts] Mark the topic unread ::  View previous topic :: View next topic
Author Message
Stingerr
Stealth Laser Trooper


Joined: 11 Jan 2005
Location: The Netherlands

PostPosted: Wed Nov 28, 2012 6:38 pm    Post subject:  Anyone good with javascript here?
Subject description: need a little help
Reply with quote  Mark this post and the followings unread

Hey guys, I have to do a little assignment in javascript for my course, but I can't seem to get my code working like it should.

It's very basic as you can see, and I think it should work, but it keeps displaying the alert for "true" even when I don't have fruit selected from the dropdown-list. What am I overlooking here? If someone could point me to it I would be very happy because I've been staring at this for like 3 hours now..

I'm just starting out with coding so things aren't always obvious for me..

Code:

<html>
<body>
   <p>Select fruits in the list:</p>
   <select>
      <option>Apple</option>
      <option>Lettuce</option>
      <option>Onion</option>
      <option>Banana</option>
      <option>Bread</option>
      <option>Tea</option>
      <option>Orange</option>
   </select>
   <br> <br>
   <input>

<script></script>
<script>
jQuery("#checkAns").click(function() {
   var isCorrect = fruitSelector.checkAns();
   
   if (isCorrect=true) {
      alert("ALL SELECTED ARE FRUITS!");
   } else {
      alert("You have not selected any fruits.");
   }
   
});

var fruitSelector = {
   fruits: [
      "apple",
      "orange",
      "pear",
      "kiwi",
      "banana",
      "grapefruit"
   ],
   checkAns: function() {
      try {
         var isFruitSelected = false,
            sel = jQuery("#theSelector");
      
         optchecker:for (var i=0;len=sel.options.length, i < len; i++) {
            if (sel.options[i].selected) {
               if (jQuery.inArray(sel.options[i].value == fruitSelector.fruits)) {
                  isFruitSelected = true;
               } else {
                  isFruitSelected = false;
                  break optchecker;
               }
            }
         }
         return isFruitSelected;
      } catch(e) {
         if (window.console && window.console.log) {
            console.error(e.message);
         }
      }
   }
};

</script>
</body>
</html>

_________________
Criticizing n00bs is like booing at the special olympics.

Back to top
View user's profile Send private message
Dutchygamer
President


Joined: 18 Jun 2005
Location: Dordrecht, the Netherlands

PostPosted: Thu Nov 29, 2012 6:21 pm    Post subject: Reply with quote  Mark this post and the followings unread

When I c/p this code to an HTML file and run it I get nothing, just the dropdown list and an empty text field which stays empty no matter what option I choose (running latest FF btw) Confused

Back to top
View user's profile Send private message Send e-mail Skype Account
0warfighter0
Commander


Joined: 07 Dec 2007
Location: Belgium, Haasdonk

PostPosted: Thu Nov 29, 2012 7:42 pm    Post subject: Reply with quote  Mark this post and the followings unread

Same here.

Back to top
View user's profile Send private message Skype Account
Nyerguds
General


Joined: 24 May 2004
Location: Flanders (Be) Posts:300000001

PostPosted: Thu Nov 29, 2012 9:11 pm    Post subject: Reply with quote  Mark this post and the followings unread

It doesn't even have a form tag around the selection box...

Also, scripts are typically kept inside the html head section, not in the body.

_________________

Back to top
View user's profile Send private message Visit poster's website Skype Account
Banshee
Supreme Banshee


Also Known As: banshee_revora (Steam)
Joined: 15 Aug 2002
Location: Brazil

PostPosted: Fri Nov 30, 2012 1:51 am    Post subject: Reply with quote  Mark this post and the followings unread

Scripts inside the body is not really a problem. I think PPM has it. A lot of sites have it to use Google Adsense. Including this forum.

Anyway, the lack of form is usually a problem, although in this case I have my doubts, since Stingerr doesn't seem to want to activate a different page/content.

The problem is that the select component doesn't seem to be used by the javascript code.

Back to top
View user's profile Send private message Visit poster's website Skype Account
Stingerr
Stealth Laser Trooper


Joined: 11 Jan 2005
Location: The Netherlands

PostPosted: Fri Nov 30, 2012 10:59 am    Post subject: Reply with quote  Mark this post and the followings unread

@ Dutchygamer,

Yeah probalby because the jQuery library is not present in this code. I'm sure I coppied and pasted it, but PPM blocks that part out because of security reasons I think.

@ banshee

Indeed. I looked at it for ages but in theory it should work. Maybe anyone knows a good plugin so I can debug this? since I don't get any errors when I use in in a browser.

_________________
Criticizing n00bs is like booing at the special olympics.

Back to top
View user's profile Send private message
0warfighter0
Commander


Joined: 07 Dec 2007
Location: Belgium, Haasdonk

PostPosted: Fri Nov 30, 2012 3:29 pm    Post subject: Reply with quote  Mark this post and the followings unread

I googled it and there seems to be a Firebug add-on for Firefox which allows you to do all kinds of Javascript (and other) debugging, inside the browser.

Back to top
View user's profile Send private message Skype Account
Dutchygamer
President


Joined: 18 Jun 2005
Location: Dordrecht, the Netherlands

PostPosted: Fri Nov 30, 2012 4:49 pm    Post subject: Reply with quote  Mark this post and the followings unread

0warfighter0 wrote:
I googled it and there seems to be a Firebug add-on for Firefox which allows you to do all kinds of Javascript (and other) debugging, inside the browser.

Firebug is really a must-have when developing/debugging Javascript (or generic website behaviour). I really recommend using it Stingerrr.

Back to top
View user's profile Send private message Send e-mail Skype Account
Stingerr
Stealth Laser Trooper


Joined: 11 Jan 2005
Location: The Netherlands

PostPosted: Sun Dec 02, 2012 4:40 pm    Post subject: Reply with quote  Mark this post and the followings unread

Thanks for the tip man!

_________________
Criticizing n00bs is like booing at the special olympics.

Back to top
View user's profile Send private message
AlexB
Commander


Joined: 31 May 2010
Location: Germany

PostPosted: Mon Dec 03, 2012 12:58 am    Post subject: Reply with quote  Mark this post and the followings unread

if (isCorrect=true)

This line is not a comparison, it is an assignment. If you want to compare, you gotta use ==. Right now, you assign isCorrect:=true (which then evaluates to true and thus the true branch of the if block is executed.

_________________

Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [10 Posts] Mark the topic unread ::  View previous topic :: View next topic
 
Share on TwitterShare on FacebookShare on Google+Share on DiggShare on RedditShare on PInterestShare on Del.icio.usShare on Stumble Upon
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © phpBB Group

[ Time: 0.1835s ][ Queries: 11 (0.0075s) ][ Debug on ]