0

I moved a site from one location to another. However i noticed when i moved it some items in my java script stop working. I have been trying to figure out where this went wrong but this is my last resort. Here is my fiddle site:

http://jsfiddle.net/robertpurpose/FCZVJ/6/

if ($("#Deg0").attr('checked') && $("#Res0").attr('checked')) {
    $("#TuitionPerHour").val(202.83);
}

if ($("#Deg0").attr('checked') && $("#Res1").attr('checked')) {
    $("#TuitionPerHour").val(202.83);
}
if ($("#Deg0").attr('checked') && $("#Res11").attr('checked')) {
    $("#TuitionPerHour").val(202.83);
}

if ($("#Deg0").attr('checked') && $("#Res2").attr('checked')) {
    $("#TuitionPerHour").val(parseFloat(243.39).toFixed(2));
}

if ($("#Deg0").attr('checked') && $("#Res3").attr('checked')) {
    $("#TuitionPerHour").val(parseFloat(304.24).toFixed(2));
}

Feel free to be as blunt as you want. We all live and learn. I take everything as a learning experience

Paradigm
  • 189
  • 1
  • 5
  • 21
  • 3
    Can you describe what is the actual vs. expected behavior? – Ruan Mendes Feb 07 '13 at 18:11
  • Your fiddle. You need to set the JavaScript to run in head and you need to set the jQuery library. – epascarello Feb 07 '13 at 18:13
  • You need to also learn what `else if` is. – epascarello Feb 07 '13 at 18:14
  • 1
    This line looks funny `parseFloat(243.39).toFixed(2)`. You're asking to `parseFloat` on something that is already a float. You probably did that because you wanted to call `toFixed` on it and you can't just use `243.39.toFixed()`. You should use either `Number(243.39).toFixed()` or `(243.39).toFixed()` – Ruan Mendes Feb 07 '13 at 18:15
  • the behavior that is expected is that once the user selects values from the top portion it should be adjusting values right through the form. Once they are done somewhat of a preview of fees should display at the bottom. – Paradigm Feb 07 '13 at 18:15
  • @epascarello: Why in head and not `onload` or down in body? – Marcel Korpel Feb 07 '13 at 18:24
  • 1
    You do some checking on the server side, too, don't you? – Marcel Korpel Feb 07 '13 at 18:25
  • @MarcelKorpel If you tried to run the code, you'll see that updateTotal is not defined, it's because it's local to the onload function. That's why you need to put it in the head, so it doesn't get wrapped in a function and makes your function private – Ruan Mendes Feb 07 '13 at 18:26
  • @MarcelKorpel I actually dont everything is through the script. – Paradigm Feb 07 '13 at 18:27
  • @MarcelKorpel This looks like an estimate, why is the server side needed? – Ruan Mendes Feb 07 '13 at 18:31
  • @JuanMendes: Oh wait, you're right. I thought it was an enrolling system. Damn! – Marcel Korpel Feb 07 '13 at 18:33
  • @Aristos `$.prop` vs `$.attr` is a very common problem and it's actually quite hard to understand and track bugs caused by it. Do your homework before you suggest that the question is not useful. See my answer. – Ruan Mendes Feb 07 '13 at 18:38
  • @Robertpurpose Please try to come up with better titles to make your question most useful. The current title suggests that this is not a good question for SO because it's asking for a code review. Title should be something like. "Script stopped working after jQuery upgrade" or "jQuery code broke after moving servers" – Ruan Mendes Feb 07 '13 at 18:41
  • Ok i will change it right now @JuanMendes – Paradigm Feb 07 '13 at 18:43
  • 1
    @Robertpurpose Did my answer not work? Any reason you haven't accepted it? – Ruan Mendes Feb 07 '13 at 18:59
  • @JuanMendes I tried all the options in Fiddle but it didnt work. Tried it on the actual form and only the top half of the form work which is just puzzling to me. But I will accept it as an answer and keep searching on my end for what could have gone wrong – Paradigm Feb 07 '13 at 19:08
  • @Robertpurpose The jsfiddle I posted doesn't work? If so, point out which part. – Ruan Mendes Feb 07 '13 at 19:09
  • @JuanMendes so sorry about that, I totally missed it. It works on point! – Paradigm Feb 07 '13 at 19:13

1 Answers1

1

The problem is that you are using $.attr('checked'). That used to return a boolean, but it now returns the attribute value (string) 'checked'. HTML - Why boolean attributes do not have boolean value?

So your check

if ($("#Deg1").prop('checked')) {
    $("#TuitionPerHour").val(294.44);
}

Is always passing. Change all your $.attr to $.prop to http://jsfiddle.net/FCZVJ/7/

Other Problems with your code

  • Your jsfiddle JS needs to be in the head so your function updateTotal is global
  • Store your jQuery variables (you're calling $("#Deg0") a million times)
  • eval(document.CostEst.FoodPlan[TheIndex_FoodPlan].value)); Crapola, using eval to convert a string to a number? Use parseInt() or parseFloat
  • $("#SemesterHours").val() <= 7 does work because of auto coercion, but note that if it said $("#SemesterHours").val() <= $('#somethinElse').val() you'd be comparing strings and "17" > "9" === false.
  • parseFloat(243.39).toFixed(2) is an eye sore (code smell). You're asking to parseFloat on something that is already a float. You probably did that because you wanted to call toFixed on it and you can't just use 243.39.toFixed(). You should use either Number(243.39).toFixed() or (243.39).toFixed()
Community
  • 1
  • 1
Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217