0

I am trying to create a dropdown list that takes two input parameters (name, IPAddress). I have declared global variable Name and IP to track which name and ip address have been selected. However, the part that I am having a trouble is that it is not recognizing the IP address. Single name can have multiple IP address that I want to recognize both name and IP address when it was selected in dropdown list :(

Any advise on this would be appreciated! Thank you!

 //called after name changed
 function selectName(){
   Name = $(#"name :selected").val();
  }

//create dropdown list for name and IPAddress
function createDropList (name, IPAddress){
  $('#name').empty();
  $('#name').append('<option disabled selected value=""> Select Name </option>');
    for (var i=0; i<IPAddress.length; i++) {
        $('#name').append('<option value= "'+name[i]+'" >'+IPAddress[i] + " (IP :"+IPAddress[i] +")" +'</option>');
  }}

1 Answer 1

1

Your code:

$('#name').append('<option value= "'+name[i]+'" >'+
    IPAddress[i] + " (IP :"+IPAddress[i] +")" +'</option>');

generates this:

<option value= "name" >1.2.3.4 (IP :1.2.3.4)</option>

You need:

$('#name').append('<option value= "'+name[i]+':'+
    IPAddress[i] + '">(IP :"'+IPAddress[i] +")" +'</option>');

to generate:

<option value= "name:1.2.3.4">(IP :1.2.3.4)</option>

In this way, your value will be the name and IP separated by a colon (:)

To set the global variable "IP" to the value when selected:

$('#name').on('change', function() {
    var value= $('#name').val().split(':');
    Name= value[0];
    IP= value[1];
}
Sign up to request clarification or add additional context in comments.

8 Comments

I want to save IPAddress to global variable IP as well so that I can pull information from database where that contains particular username and IP Address. How do I have to do in this case..?
I have updated my answer. BTW, using global variables is bad- you should try to avoid using globals.
When I set IP global variable like you told me, I just get results that are based on that IP address. When I do "Name = $(#"name :selected").val();" , my result is just based on username. When I set both, it just not work :( Why can't I set both Name and IP on selected value? I want the results to be both depend on username and ip address :(
OK, I'm sorry, I don't understand what you're trying to accomplish.
how do I just save username itself?
|

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.