-5

Right I have a jQuery function which I am struggling to get working. There is a Select drop down menu, when the "Fat Quarter" is selected, I want it to half the value "a" in the jQuery function below

HTML:

<select data-unit="m" data-common-unit="m" name="length_needed" id="length_needed" class="amount_needed">
    <option value="0.500">Fat Quarter</option>
    <option value="0.25">      1/4m</option>
    <option value="0.5">      1/2m</option>
    <option value="0.75">      3/4m</option>
    <option value="1">      1m</option>
</select>

jQuery:

jQuery(document).ready(function(a) {    
function g(a) {
    var b = "",
        d = decimals,
        e = pos,
        f = symbol;
    switch (.....) {
        case "left":
            if ("input.amount_needed".value == "Fat Quarter"){
                b = '<span class="amount">' + f + a/2 + "</span>";
            }else{
                b = '<span class="amount">' + f + a + "</span>";
            }
            break;
        ...
     }
}

Basically the if statement is at fault somewhere, and I cannot figure it out - it shows the price at full (not divided by 2).

Full code can be seen here: https://jsfiddle.net/avfsdL9x/

It works fine without the if statement in place - it is a mixture of html, php, jquery, but I cannot put all the PHP in as its split across 100s of files.

James Simpson
  • 197
  • 2
  • 16
  • 3
    `"input.amount_needed".value` Missing something? Hint: It's currency symbol that you're missing if you want to use it :P – Tushar Oct 03 '15 at 16:27

2 Answers2

5

Do you need to do something like this?

if ($("input.amount_needed").val() == "0.500") {

Here the 0.500 and 0.5 will be treated as different strings, so you can trust on them. Wow, I took about half an hour to understand what you wanna tell!

And I am not sure you need to put it in a switch case statement here. Just binding it in onchange event will work out.

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
  • _Question is totally not clear. – Praveen Kumar_ and yet you're able to answer it – Tushar Oct 03 '15 at 16:47
  • @Tushar I retracted my close vote. Forgot to delete the comment! `:P` – Praveen Kumar Purushothaman Oct 03 '15 at 16:48
  • This may solve one problem of OP, but look at code there are many problems – Tushar Oct 03 '15 at 16:52
  • @PraveenKumar - the code has been shortened down to the min, there are bits in there that are missing but not needed for this issue - i am now getting Uncaught TypeError: $ is not a function – James Simpson Oct 03 '15 at 17:00
  • @JamesSimpson That mean you haven't loaded jQuery on your page or you're using `$` before the jQuery is loaded – Tushar Oct 03 '15 at 17:01
  • @JamesSimpson Looks like you aren't loading jQuery library. – Praveen Kumar Purushothaman Oct 03 '15 at 17:01
  • @PraveenKumar - it must do as everything works without that if statement in place. – James Simpson Oct 03 '15 at 17:02
  • @JamesSimpson _the code has been shortened down to the min, there are bits in there that are missing but not needed for this issue_ this has caused you **7** downvotes. – Tushar Oct 03 '15 at 17:03
  • @JamesSimpson That's because your question is not clear. Also I was confused why you are complicating by using `switch` and `case`. But if the issue is that, then mine's the solution. – Praveen Kumar Purushothaman Oct 03 '15 at 17:04
  • @Tushar Yes, my answer is shortened down to the min, those you were talking are missing but not needed for this issue! `:P` – Praveen Kumar Purushothaman Oct 03 '15 at 17:05
  • @PraveenKumar - full code here - this is done by someone else, the only bit I am adding is the If Statement we are talking about above, which I cannot get to work, it always goes to the "else" statement https://jsfiddle.net/avfsdL9x/ – James Simpson Oct 03 '15 at 17:07
  • Wow! 2 upvotes. I wonder How? in question ` – Tushar Oct 03 '15 at 17:08
  • @JamesSimpson Did you try adding a dummy `` to really trigger the event? – Praveen Kumar Purushothaman Oct 03 '15 at 17:09
  • @JamesSimpson If the code is long, you should have mentioned that in question and added a fiddle link, like you've done now. That could have saved many down votes. Anyway, keep in mind when asking questions here onwards. :) – Tushar Oct 03 '15 at 17:10
  • This bit is really wrong: `"input.amount_needed".value` can you please change it at least to `jQuery("input.amount_needed").val()`? – Praveen Kumar Purushothaman Oct 03 '15 at 17:10
  • @Tushar - Will do, and i appreciate all this help, its been annoying me for 2 days now. – James Simpson Oct 03 '15 at 17:11
  • @PraveenKumar - basically this is a mixture of php, html and jquery that works out the price and passes the value to a span (amongst other things). When I add $ into the if statement i get the following error: Uncaught TypeError: $ is not a functiong @ wc-measurement-price-calculator.min.js?ver=4.3.1:43(anonymous function) @ wc-measurement-price-calculator.min.js?ver=4.3.1:95m.event.dispatch @ jquery.js?ver=1.11.3:4m.event.add.r.handle .... – James Simpson Oct 03 '15 at 17:13
  • @JamesSimpson I am really trying to solve this, if you make it really simple. Is this issue only limited to `JavaScript`? If not, please add the PHP part too. Or can you tell what exactly are you trying to do? – Praveen Kumar Purushothaman Oct 03 '15 at 17:14
  • @JamesSimpson That means you've loaded the plugins that depends on the jQuery before jQuery, load jQuery first and then all its dependents. – Tushar Oct 03 '15 at 17:16
  • @PraveenKumar - unfortunately the php is split across like 100 different files, so I can't post that - I would have coded it completely different. – James Simpson Oct 03 '15 at 17:20
  • @Tushar - I don't get how its working in the first place then (without the if statement). :S – James Simpson Oct 03 '15 at 17:20
  • @JamesSimpson What do you wanna do exactly, don't talk about the code. Say the workflow. – Praveen Kumar Purushothaman Oct 03 '15 at 17:21
  • 1
    @JamesSimpson Now this is getting painful, we're trying to help you and you don't wanna share code. How are we supposed to solve the problem? And yes, if you have the question other than this above which is not related to this please ask another question. – Tushar Oct 03 '15 at 17:23
  • @PraveenKumar - Basically the code at the moment, when you change the measurement selection, it will update the selection price - so when you select 1m it says £9.80, 1/2m - £4.90, 1/4m - £2.45 - The issue is the Fat Quart, as it needs to remove 0.5m, but be the same price as 1/4m - Ive done this in php code, but having issues with the jquery side of things. It shows as £4.90 at the moment, but I need it to be half that at £2.45. – James Simpson Oct 03 '15 at 17:33
  • @PraveenKumar would a link to the site help? – James Simpson Oct 03 '15 at 17:34
  • @JamesSimpson Yeah buddy. – Praveen Kumar Purushothaman Oct 03 '15 at 17:36
  • @PraveenKumar - this is a link to my mothers live site - http://www.newforestfabrics.co.uk/shop/fabrics/fabric-collections/free-spirit-fabrics/eclectic-elements-2/music-in-neutral/ Basically this is coded wrong (as the fat quarter only takes of 1/4 of stock) - You can see it working when you change the selection here - I have updated it to 0.500 on my development on my pc but it then shows the price of half a meter (as you would with a value of 0.500) – James Simpson Oct 03 '15 at 17:45
  • @JamesSimpson Please give a dummy value like as I said before inside the ` – Praveen Kumar Purushothaman Oct 03 '15 at 17:47
  • @PraveenKumar - Not sure what you mean, as the dummy values are above - the first option is always Fat Quarter. So as i now have fat quarter value as 0.500 - the jquery thinks its half a meter price, but it needs to take half a meter off the stock but charge the same price as the 1/4m - i.e. if("input.amount_needed".value == "Fat Quarter") - it will half the displayed price (of 0.5m) – James Simpson Oct 03 '15 at 18:04
  • @PraveenKumar I think it has something to do with this http://stackoverflow.com/questions/12343714/typeerror-is-not-a-function-when-calling-jquery-function Looking at the opening of the jquery jQuery(document).ready(function(a) { – James Simpson Oct 03 '15 at 18:47
0

Finally got it working - the instance wouldnt work with the $ as it required jQuery to be there instead

if (jQuery("select.amount_needed").val() == "0.500") {
                b = '<span class="amount">' + f + a/2 + "</span>";
            }else{
                b = '<span class="amount">' + f + a + "</span>";
            }
}
James Simpson
  • 197
  • 2
  • 16