0

I'm trying to call the JavaScript function on automatically selected value from dropdown options on the basis of what value came from DB. Right now JavaScript function got called when I change options from the front-end. Here is my code. What I want is to call JavaScript Getstate(this.value) function on automatically selection which depends on the value came from DB. Any suggestions?

 <label>Province: </label><br>
 <select name="Provincedown" id="ProvinceDropDown"  class="form-control" onChange="getState(this.value);">
 <option value="SelectProvince" <?=$ProvinceName == 'SelectProvince' ? ' selected="selected"' : '';?> > Please Select </option>
 <option value="Sindh" <?=$ProvinceName == 'Sindh' ? ' selected="selected"' : '';?>> Sindh </option>
 </select>

getState(this.value) is my JavaScript function in which I'm using AJAX and making my data JSON and sending to another page.

8
  • what you mean with 'on selection' ? Commented Sep 5, 2016 at 21:40
  • @Carlo I'm trying to say that gestate function get call whether there is a change in value from front end but here I want to call getstate() function when I automatically select value on the basis of the value I got from DB like I'm doing in my code Commented Sep 5, 2016 at 21:46
  • So you want to fire the first time on page load, where the selected value is printed by the server? It should be as symple as calling getState(document.getElementById("ProvinceDropDown").value) on page load Commented Sep 5, 2016 at 21:56
  • @Carlo so you want me to change this.value to document.getElementById("ProvinceDropDown").value ???? Commented Sep 5, 2016 at 22:00
  • no. in the onChange property it's fine as it is. You have to call the function on page load, with the selected value as a parameter. I sense that you will understand this better if you remove the scripts from inline and include them in a js file Commented Sep 5, 2016 at 22:02

1 Answer 1

3

Use window.onload to get your selected value from dropdown.

<label>Province: </label><br>
 <select name="Provincedown" id="ProvinceDropDown"  class="form-control">
 <option value="SelectProvince" <?=$ProvinceName == 'SelectProvince' ? ' selected="selected"' : '';?> > Please Select </option>
 <option value="Sindh" <?=$ProvinceName == 'Sindh' ? ' selected="selected"' : '';?>> Sindh </option>
 </select>


 <script>
 function abc()
 {
 var name=document.getElementById("ProvinceDropDown").value;
 alert(name);
 }
 window.onload = abc;
 </script>
Sign up to request clarification or add additional context in comments.

1 Comment

that is exactly i did yesterday night.; Thankyou (:

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.