1

I try to Run Default Function on HTML Select, but Default Function doesn't works for Default Selected Value, But it Works On User Select

<?php
    $status_selected = 'A002';
?>
<!-- HTML Select 1 -->
<select id="state" class="l" name="state" onchange="Func()">
<option value="A001" <?php if($status_selected == "A001") echo "selected"; ?> data_item=" , StateA003 One, A003 State Two, A003 State Three">A001</option>
<option value="A002" <?php if($status_selected == "A002") echo "selected"; ?> data_item=" , A003 State One, A003 State Two, A003 State Three">A002</option>
<option value="A003" <?php if($status_selected == "A003") echo "selected"; ?> data_item=" , A003 State One, A003 State Two, A003 State Three">A003</option>
</select>
<!-- HTML Select 2 -->
<label for="city">Item : </label><select id="city" name="item" class="l"  onchange="onSelected()">

<script>
    function Func() {
        var city = document.getElementById('item');
        var state = document.getElementById('state');
        var val=state.options[state.selectedIndex].getAttribute('data_item');
        var arr=val.split(',');
        item.options.length = 0;
        for(i = 0; i < arr.length; i++) {
            if(arr[i] != "") {
                item.options[item.options.length] = new Option(arr[i],arr[i]);
            }
        }
    }

    function onSelected() {
        var item = document.getElementById('item').value;
        var state = document.getElementById('state').value;
        console.log('Parent : ' + state+ ', Item : ' + item);
    }
</script>

it load HTML Select One And then Select Item in HTML Select Two, But HTML Select Two Works Only By Manual User Select And Not Works For Default Programatically Value

Whole Of Codes Above Are On PHP File.

How i can fix it ?

1
  • what is Default Function? Commented Jun 11, 2016 at 13:57

2 Answers 2

2

Not sure if it is relevant, but maybe you want a chained select box? If not, please clarify your question, it's very hard to follow.


EDIT: So the real issue is selecting the right option by code. In that case, this is a duplicate of How do I programmatically set the value of a select box element using javascript?.

In your onload script(or just after the functions if the script is at the bottom of the page), also run Func(), then set the value for #city:

Func();
var state = document.getElementById('state'),
    city = document.getElementById('city'),
    selectedCity = state.getAttribute('data-selected-city');
if(selectedCity) {
    city.value = selectedCity;
}

You need to store the selected city somewhere, my suggestion is to add it to the select:

<select id="state" data-selected-city="new-york">
<!--and so on--->
</select>

NOTE: setting a data attribute in HTML5 should begin with data-, not an underscore. So it's data-item="", not data_item="". That will give you a warning when running HTML validator.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks, but my issue is I want to choose Item programmatically i can't find solution, but by user Selected it works well
1

Put Out Of PHP Tag:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>

On Script Tag Put :

$(function() {
    $("#state").on("change", Func()); 
});

After :

item.options[item.options.length] = new Option(arr[i],arr[i]);

put the code :

$(item).prop("selectedIndex",i);

1 Comment

Yes, It Works, Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.