0

I am trying to get the data from database and display it in select option through drop down menu. I am getting the data correctly in java script page as well. But it is not displaying in the drop down menu of html.

HTML code:

<td>Organization Name:</td>
<td>
    <select id="orgname" name="Oname" onload="getOrganizationname();">
        <option selected disabled value=" ">Choose your Organization</option>

        <!-- <option value="1" >xyz</option>
        <option value="2">Brocade</option>
        <option value="3">Citrix</option> --> 
    </select>
</td>

Javascript code: function getOrganizationname() {

$.get('getOrganizationname.jsp',function(data)
{
    document.getElementById("orgname").innerHTML= data;
        alert(data);
    }
    );
}

Onload I am getting this in alert msg(trying to debug)

This is the information from the database:

<option value="Infosys"></option>
<option value="Akamai"></option>
<option value="Brocade"></option>
<option value="XYZ"></option>
<option value="XYZ1"></option>
<option value="XYZ2"></option>
3
  • onload i am getting this in alert msg(trying to debug) This is info. from database: <option value="Infosys"></option> <option value="Akamai"></option> <option value="Brocade"></option> <option value="XYZ"></option> <option value="XYZ1"></option> <option value="XYZ2"></option> Commented Mar 2, 2017 at 10:18
  • So watever data i am getting as value, I want to it to be between the option like : <option value="Infosys">Infosys</option>....... like this Commented Mar 2, 2017 at 10:20
  • You're use of $.get would appear to be jQuery. Is this correct? If so, I'll tag your question as such... Commented Mar 3, 2017 at 21:11

2 Answers 2

1

You use use:

$(document).ready(function(){
    $.get('getOrganizationname.jsp',function(data){
        $("#orgname").html(data);
    });
})

So your code now becomes:

<td>Organization Name:</td>
<td>
    <select id="orgname" name="Oname">
        <option selected disabled value=" ">Choose your Organization</option>
    </select>
</td>
<script>
$(document).ready(function(){
    $.get('getOrganizationname.jsp',function(data){
        $("#orgname").html(data);
    });
});
</script>
Sign up to request clarification or add additional context in comments.

Comments

0

After you got data and set it, does your html looks like this? http://codepen.io/anon/pen/ryLaxV

If yes, the problem is that you haven't text inside your options.

<option value="Infosys"></option>

should become

<option value="Infosys">Infosys</option>

How to?

var jData = $(data) // create jquery wrapped dom elements

// now iterate them and add text from values
jData.each(function () {
  var currentElement = $(this) // that's jquery way...
  var value = currentElement.val()
  currentElement.text(value)
})
// finally append it to the select element
$('#orgname').html(jData)

Also, consider the suggestion of using $(document).ready by Shakti Phartiyal.

And please, use console.log(data) for debug. To see the console - right mouse click on your page --> inspect element.

P.S. it would be much better, if you got raw data from server, I mean a json or something like that! :)

Comments

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.