Jump to content

drop down value print for dynamic data


s_avinash_s

Recommended Posts

Hi 

I am trying to print the value in textbox which is selected from dropdown.

First 2 drop down values am able to print in text box as it is static value.

For third value  , am getting from server.

 how should i print the third after receiving from server.

<!DOCTYPE html>
<html>
<body>

<select onchange="Choose(this)" >
  <option value="2" >temp1</option>
  <option value="22" >temp2</option>
  <option value="temp3">temp3</option>
  <option value="temp4" >temp4</option>
</select>
<input type="text" id="answer" >


<script>
function Choose(data) {
   
    document.getElementById ("answer").value = data.value;
}

function absolute() {

      var xhttp = new XMLHttpRequest();
      xhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) { 
	      document.getElementById("demo2").innerHTML = this.responseText;        
        }
      };
    xhttp.open("GET", "absolute", true);
    xhttp.send();
    }

    setInterval(absolute, 1000);
</script>


 <p  id="demo2"></p>


</body>
</html>

 

Link to comment
Share on other sites

If you're asking how do I update my select to be populated with server values:
Scenario:

<select>
  <option value="[serverValue]">[ServerName]</option>
</select>

Its not going to be pretty. In this scenario if you wanted to (on the fly) change what this select is you'll need to:
Need to manually change the InnerHTML property, remember which one was selected, stop the refresh while the user has the dropdown open. This is also non-standard and I do not recommend it. (NOTE: If this only needed to be populated on page load, I suggest a preprocessing language like PHP, or otherwise)

 

If you're asking for a server value based on what is selected, ie select 'temperature' get 60F or whatever it is at the time, then you may want to have a look at sending values. This would be better.

<select name="action">
  <option value="temperature">Temperature</option>
  <option value="height">Height</option>
</select>

You would then receive the value from the server based on what you sent to the server.

Here's a good link for that: https://developer.mozilla.org/en-US/docs/Learn/HTML/Forms/Sending_forms_through_JavaScript
 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...