• Resolved cenay

    (@cenay)


    Appreciate any help you can provide. I am using PPOM to display custom meal options. When I added the “9th” item to Protein, it doesn’t get an ID assigned. As I am using that ID to perform some calculations (display the macros), it’s imperative I get that value.

    I did some testing, and it’s the “final” one I add. Another note. I added 9 elements, but your id’s start at 1 (skipping the “Choose an option” as 0). I thought at first it was the double digit breaking it when it got to 10. But when I added one more (for a total of 10 items and 11 id’s), the final one displays. Please see this video to see the exact issue.
    https://www.screencast.com/t/EhCXW3lfY

    Thanks in advance for your timely help, I have a client waiting on this.

    The page I need help with: [log in to see the link]

Viewing 8 replies - 1 through 8 (of 8 total)
  • Thread Starter cenay

    (@cenay)

    So, here’s a little more info and a strange but true situation. Recorded it for you. https://www.screencast.com/t/nyHOLYv6XuX

    I’m not sure where those IDs are being generated. It’s not in the nmInput.class.php file for select fields however I know an easy solution to what you are trying to do.

    By looking at your source code of the inline jQuery you’ve placed you should refrain from referencing the ID from each select’s option. You should reference the value or the text of the option selected.

    Try using $("#id_of_select_field option:selected").text() or $("#id_of_select_field option:selected").val() instead. If you need to use a loop $(this) then use $("option:selected",this).text() or $("option:selected",this).val().

    When coding custom scripts to interact with the third-party scripts you should always avoid using any of the custom attributes that they generate as they can change without warning. Values and text strings are what you specify and are the safest to reference. Hope this helps.

    Also, if you are trying to associate the option selected with an index in an array you could also use the .index() method.

    That is difficult to coordinate though as your array would have to perfectly match your PPOM fields. IMO, it would be best just to use a switch / case scenario based on select input IDs and the option value / text.

    Hi @cenay,

    Yes, I am surprised how/where the “id” attribute is being added options. It’s somehow customized and Sorry to say we don’t provide support if you have made changes in PPOM. You need to make sure it’s clean installed and then if you have any issue please let us know here.

    Thanks @brozra.

    Thread Starter cenay

    (@cenay)

    Interesting. The javascript written to calculate the macro’s on the custom meals page was written by another dev, and my requirement was that he NOT change the plugin in any way. He claimed that was true. He is no longer available.

    To my knowledge (based on the fact that I am able to update the plugin as new versions come out with no ill effects) there aren’t any plugin changes or customization’s at all. This code has been in place 18 months now and the latest update is the only one that has “broken” the calc’s.

    Since we aren’t inserting the id’s, can you explain why the final element is the only one that doesn’t get an id? I’ve added to the PPOM list for protein and taken away an element or two, and the last element is the only one that doesn’t get the id.

    And to verify the clean install, I deleted the plugin and installed it again and the problem remains.

    Yes, I can see now. The PPOM plugin itself on your website is not customized but the script (what you call a macro) your previous developer created is what is causing your problem.

    The offending code is here:

    
            $.each($('#protein option'), function(ind) {
               $(this).attr('id', '' + parseInt(ind + 1));
            });
    
            $.each($('#vegetable option:not(:first)'), function(ind) {
               $(this).attr('id', '' + parseInt(ind + 1));
            });
    
            $.each($('#vegetable_size option'), function(ind) {
               $(this).attr('id', '' + parseInt(ind + 1));
            });
    
            $.each($('#carb_grain option:not(:first)'), function(ind) {
               $(this).attr('id', '' + parseInt(ind + 1));
            });
    
            $('#protein option:last-child').removeAttr('id');
            $('#vegetable option:last-child').removeAttr('id');
            $('#vegetable_size option:last-child').attr('id', '0');
            $('#carb_grain option:last-child').removeAttr('id');
            $('#carb_grain_size option:last-child').attr('id', '0');
    

    The $.each code traverses each individual select option and adds the ID field to each individual option under each select. Although the parseInt is out of whack. It should be $(this).attr('id',(parseInt(ind)+1));.

    Then, for some reason, the last option ID is removed. This code:
    $('#protein option:last-child').removeAttr('id');
    This is exactly why you don’t see an ID on the last attribute.

    Additionally, the reason that the calories and counts are out of whack is because the arrays for referencing the values have to be updated as well when you add a new PPOM meta field value for any of the options. So if you add a new protein, you have to update the var proteinArray with a JSON-encoded value that matches the new value that you are adding in PPOM. This also applies to the vegetableArray and carbArray.

    The script (macro) was written to return zeroes for everything if nothing was found. That is why you are seeing all zeroes for the new options you are adding.

    Your problem is not a PPOM plugin issue. It is your custom script that interacts with the PPOM fields. It needs to be revised to work properly in-accordance-with the changes you are making to the PPOM selection fields.

    Thanks, @brozra for detailed analysis. And the bottom line is to remove or revise extra code added for PPOM.

    Thread Starter cenay

    (@cenay)

    Yes, thanks @brozra for that help. I did make some of the changes you suggested and removed the unnecessary code to remove the id on the final element.

    I’ve contacted my new developer with this thread and the custom code. With your suggestions about using the $("#id_of_select_field option:selected").text() instead, I think we’ll be able to create a more stable solution.

    Appreciate you both for your help.
    Cenay’

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘10th element in a field does not get an “id”’ is closed to new replies.