2

I have written a html code for two dropdown boxes. I want to pass these two values as a parameters to a javascript function when we change the second dropdown (onchange).

Here is my code:

function CatName(str1, str2) {

  if (str == "") {
    document.getElementById("album_name").innerHTML = "";
    return;
  } else {
    if (window.XMLHttpRequest) {
      // code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp = new XMLHttpRequest();
    } else {
      // code for IE6, IE5
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function() {
      if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        document.getElementById("album_name").innerHTML = xmlhttp.responseText;
      }
    };
    xmlhttp.open("GET", "update.php?q=" + str1 + "&q1=" + str2, true);
    xmlhttp.send();
  }
}
<div class="col-xs-6 col-sm-6 col-md-4 col-md-offset-1">
  <div class="form-group">
    <select class="form-control" name="category_name">
      <option value="" selected>Please Select</option>
      <option value="Birds">Birds</option>
      <option value="Notinlist">Category Not in list</option>
    </select>
  </div>
</div>
<div class="col-xs-6 col-sm-6 col-md-4 col-md-offset-1">
  <div class="form-group">
    <select class="form-control" name="album_name" onchange="CatName(this.value)">
      <option value="" selected>Please Select</option>
      <option value="Dove">Dove</option>
      <option value="Parrot">Parrot</option>
      <option value="Notinlist">Category Not in list</option>
    </select>
  </div>
</div>

1
  • 3
    Just do something like this : CatName(this.value, document.getElementById("id-of-the-first-select-box-here").value); Commented Feb 27, 2016 at 7:15

5 Answers 5

2

Its complicated to passing parameter and receiving them in function. A better option is to write one function and fetch these values using respective id or selector.

Sample Code

function getvalue() {
  var fval = document.getElementById('category_name').value;
  alert(fval);
  var sval = document.getElementById('album_name').value;
  alert(sval);
}
<body>
  <div class="col-xs-6 col-sm-6 col-md-4 col-md-offset-1">
    <div class="form-group">
      <select class="form-control" id="category_name">
        <option value="" selected>Please Select</option>
        <option value="Birds">Birds</option>
        <option value="Notinlist">Category Not in list</option>
      </select>
    </div>
  </div>
  <div class="col-xs-6 col-sm-6 col-md-4 col-md-offset-1">
    <div class="form-group">
      <select class="form-control" id="album_name" onchange="getvalue();">
        <option value="" selected>Please Select</option>
        <option value="Dove">Dove</option>
        <option value="Parrot">Parrot</option>
        <option value="Notinlist">Category Not in list</option>
      </select>
    </div>
  </div>
</body>

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

4 Comments

Please explain your changes as well.
i can understand what you mean?
You should explain what changes you have made that OP had missed. Why his code was not working etc. Just code is not a good answer.
ok its complicated to passing parameter and receiving them in function so write one function it automatically fetch value using respective id
1

Giving an id for the first dropdown and get the first dropdown value by that id. here i give an id "categoryName" for the first dropdown.

<div class="col-xs-6 col-sm-6 col-md-4 col-md-offset-1">
<div class="form-group">
         <select class="form-control" name="category_name" id="categoryName" > 
            <option value="" selected>Please Select</option>
            <option value="Birds">Birds</option>
            <option value="Notinlist">Category Not in list</option>
         </select>
</div>
</div>

Getting the first dropdown value by id as a parameter in onchange method.

<div class="col-xs-6 col-sm-6 col-md-4 col-md-offset-1">
<div class="form-group"  >
         <select class="form-control" name="album_name" onchange="CatName(this.value,document.getElementById('categoryName').value)">
            <option value="" selected>Please Select</option>
            <option value="Dove">Dove</option>
                <option value="Parrot">Parrot</option>
            <option value="Notinlist">Category Not in list</option>
         </select>
</div>
</div>

1 Comment

Kindly explain what have you changed as well.
0

Check this out:

$('.form-group select').change(function(){
    var val = $(this).val();
 function CatName(val) {
    alert(val);
}
CatName(val);
});

JSFiddle

Comments

0

Change the HTML of your second select statement to this:

<select class="form-control" name="album_name" onchange="CatName(this.value, document.querySelector('select[name=category_name]').value)">

First, in order to pass two values, you need to actually pass the two values into your function. In the example you provided you had only passed the value of the 2nd select element.

Second, we need to get the value of the first select element, which we only know the name of. We can use the document.querySelector() function to get the first item that matches a given CSS selector. In this case, our selector would be select[name=category_name]. Then you get the value of that element with the .value property.

document.querySelector('select[name=category_name]').value

Comments

0

Since you have mentioned jQuery, you can have a onChange handler for second dropdown and on change, fetch first select's value using name or id.

Also, you can use $.get() or $.ajax() instead.

var url = "update.php?q=" + str1 + "&q1=" + str2;
$.get(url, function(response){ // sucess handler })
  .error(function(exception){ // process exception });

$("[name='album_name']").on("change", function CatName() {
  var category = $("[name='category_name']").val();
  var album = $(this).val();
 
  $("#result").html("<pre>" + JSON.stringify({category:category, album:album},0,4));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<div class="col-xs-6 col-sm-6 col-md-4 col-md-offset-1">
  <div class="form-group">
    <select class="form-control" name="category_name">
      <option value="" selected>Please Select</option>
      <option value="Birds">Birds</option>
      <option value="Notinlist">Category Not in list</option>
    </select>
  </div>
</div>
<div class="col-xs-6 col-sm-6 col-md-4 col-md-offset-1">
  <div class="form-group">
    <select class="form-control" name="album_name">
      <option value="" selected>Please Select</option>
      <option value="Dove">Dove</option>
      <option value="Parrot">Parrot</option>
      <option value="Notinlist">Category Not in list</option>
    </select>
  </div>
</div>

<p id="result"></p>

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.