Posted: Wed Nov 28, 2012 6:38 pm Post subject:
Anyone good with javascript here?
Subject description: need a little help
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");
Joined: 18 Jun 2005 Location: Dordrecht, the Netherlands
Posted: Thu Nov 29, 2012 6:21 pm Post subject:
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) QUICK_EDIT
Also Known As: banshee_revora (Steam) Joined: 15 Aug 2002 Location: Brazil
Posted: Fri Nov 30, 2012 1:51 am Post subject:
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. QUICK_EDIT
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. QUICK_EDIT
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. QUICK_EDIT
Joined: 18 Jun 2005 Location: Dordrecht, the Netherlands
Posted: Fri Nov 30, 2012 4:49 pm Post subject:
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. QUICK_EDIT
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. _________________ QUICK_EDIT
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