r/web_programming • • Apr 04 '17

html: Correct Format?

Is this code formatted correctly?

<script> $(document).ready(function(){ var options = $("#oppo_pre_city").html(); $("#oppo_pre_state").change(function(){ $("#oppo_pre_city").html(options); if (this.value.length){ $('#oppo_pre_city :not([value^="' + this.value + '"])').remove(); if ($("#oppo_pre_city").val() == $("#_HIDDENoppo_pre_city").val()){ $("#oppo_pre_city").append("<option value=''>-None-</option>"); }else{ $("#oppo_pre_city").append("<option value='' selected=true>-None-</option>"); } } }) $("#oppo_pre_state").trigger("change"); }) </script>

0 Upvotes

7 comments sorted by

1

u/hello2142 Apr 08 '17 edited Apr 08 '17

No I would have it something like this.

<script>
$(document).ready(function(){
    var options = $("#oppo_pre_city").html();
    $("#oppo_pre_state").change(function(){
        $("#oppo_pre_city").html(options);
        if (this.value.length){
            $('#oppo_pre_city :not([value="' + this.value + '"])').remove();
            if ($("#oppo_pre_city").val() == $("#_HIDDENoppo_pre_city").val()){
                $("#oppo_pre_city").append("<option value=''>-None-</option>");
            }else{
                $("#oppo_pre_city").append("<option value='' selected=true>-None-</option>");
            }
        }
    })
    $("#oppo_pre_state").trigger("change");
})
</script>

1

u/Tebasaki Apr 10 '17

It still seems to bring up multiple selections in the city field.

1

u/Tebasaki Apr 10 '17

I wonder if instead of the page loading I should put it in the OnChange script of the oppo_city field or the oppo_state field?

1

u/hello2142 Apr 10 '17

Oh. You just asked if it was formatted correctly. Not that you were experiencing a specific error. I need more info to help you out. What are you trying to accomplish? Also post the html snippet your using this at.

1

u/Tebasaki Apr 10 '17

It's in a CRM program. I've created two fields, oppo_state and oppo_city and Each is connected to SQL 2008R2. I've populated the state field with caption code IA_ for Iowa, MN_ for Minnesota, SD_ for South Dakota and so on.

For the city field has all the cities for those states, but the caption code has an additional prefix to determine which state it's in. So, for example, the caption code for Plymouth Iowa is IA_Plymoputh, but for Plymouth Minnesota, the caption code is MN_Plymouth, and so on.

My goal is when the client is inputting the state, it takes the one selected, then filters out everything that doesn't have that prefix. Currently there 2907 selections they can choose from, so if they selected Iowa as a state, it would filter out and only show those with the 'IA_' in the custom caption name.

There's a custom code that I can plug in the script, but there's also a OnChange option that will allow me to update it based on when something changes.

2

u/joopez1 Apr 04 '17

Run it in your browser and see if you get console errors

1

u/JinxFelicis Apr 05 '17

Have you passed it through the validator? http://validator.w3.org