Dropdown 1 value automatically selects dropdown 2 value

I am trying to write a JavaScript function where if value 1 from the first dropdown(pcount) automatically selects value 1 for drop down 2(listedname), But then if anything besides value 1 (2,3,4) is chosen I do not want drop down 2 to do anything except default back to please select if value 1 was selected and then changed to another value. I am very new to JavaScript and programming in general and have not been able to find any examples similar to this. So any help will help!

JavaScript Funtion:

<script type="text/javascript">
function leaveChange() {
    if (document.getElementById("pcount").value = 1){
        document.getElementById("listedname").value = 1;
    }else
        document.getElementById("listedname").value = 2;

    }
}
</script>

Dropdown 1 and 2:

<select id="pcount" onchange="leaveChange()">
  <option value="" selected="selected">Please Select</option>
  <option value="1">0</option>
  <option value="2">1</option>
  <option value="3">2</option>
  <option value="4">3</option>
</select>

<select id="listedname">
  <option value="" selected="selected">Please Select</option>
  <option value="1">Business Name</option>
  <option value="2">Individual Owner</option>
</select>

Answers


If you are doing your if clause with one equals sign, Javascript will check if your element is set to the new value successfully.

Instead, when you do a comparison, use double equal signs (in your case)

if (document.getElementById("pcount").value == 1){

If anyone is looking for the JavaScript function here you go!

<script type="text/javascript">
function leaveChange() {
    if (document.getElementById("pcount").value == 1){
        document.getElementById("listedname").value = 1;
    }     
    else if (document.getElementById("pcount").value != 1){
        document.getElementById("listedname").value = "";

}
}
</script>

Need Your Help

SetInterval and clearInterval issues

javascript jquery setinterval clearinterval

I'm having trouble with this, my code is (simplified):

listView with two buttons (Android)

button android-listview

I have a big problem with ListView. I have 2 buttons, btPlus and btMinus (and 4 TextViews) in each listView's row. The two buttons increase (btPlus) and decrease (btMinus) the value of the label lb...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.