1

i am trying to find a way to allow a user to click items in a combo box and have its value populate an input field and also alert "work stop" or "work start" message when appropriate option is selected. But my code is not working. Please Help! Here is my code:

    <html>
    <head>

    </head>
    <body>

    <form>
<select name="sel1" onChange="populateField(this.form)" >
    <option value="">---Select---</option>
    <option value="stop" >Stop</option>
    <option value="start">Start</option>
</select>

<input type="text" id="eStop" name="eStop" />
</form>
<script type="text/javascript">

function populateField(frm){
 test = frm.stop.value;
 alert('work' test);
 frm.eStop.value = test;
}
</script>
    </body>
    </html>

Thanks in Advance

4 Answers 4

2

http://jsfiddle.net/snHQY/

<form>
    <select name="sel1" id="select" onchange="populateField();" >
        <option value="">---Select---</option>
        <option name="stop" value="stop" >Stop</option>
        <option name="start" value="start">Start</option>
    </select>

    <input type="text" id="eStop" name="eStop" />
</form>

<script>
function populateField() {
    test = document.getElementById('select').value;
    alert(test);
    document.getElementById('eStop').value = test;
}
</script>
Sign up to request clarification or add additional context in comments.

Comments

0

You can do it using the selectedIndex if you want as well,

<form>
    <select name="sel1" id="select" onchange="populateField(this);" >
        <option value="">---Select---</option>
        <option name="stop" value="stop" >Stop</option>
        <option name="start" value="start">Start</option>
    </select>

    <input type="text" id="eStop" name="eStop" />
</form>

<script>
function populateField(sel) {
  sel.form.eStop.value = 'work ' + (sel.selectedIndex == 1 ? 'Stop' : 'Start');
}
</script>

http://jsfiddle.net/cnpuM/

Comments

0

Your code has two errors. You can not reference an element by its value like this test = frm.stop.value; instead use test = frm.sel1.value; . Another error is in this line alert('work' test);. Here you are joining a string "work" with a variable "test". In java script where ever you join two or more variables or strings and variables you alway have to join them with + sign like this:alert('work ' + test);. Remaining code is ok:

<html>
<head>

</head>
<body>
<form>
<select name="sel1" onChange="populateField(this.form)" >
<option value="">---Select---</option>
<option value="stop" >Stop</option>
<option value="start">Start</option>
</select>

<input type="text" id="eStop" name="eStop" />
</form>
<script type="text/javascript">
function populateField(frm){
    var test = frm.sel1.value;
 alert('work '+ test);
frm.eStop.value = test;
}
</script>

</body>
</html>

You can also use selectedIndex property of "sel1" to do the same.

Comments

0

in your select you can add to onchange's and onkeypress to the populateField's function this as the objects reference.

<select name="sel1" onchange="populateField(this)" onkeypress="populateField(this)">

Now you can reference the select from o. to pass the selected value to the alert dialog and the input field.

function populateField(o){

  alert("work " + o.value);
  // use regular W3C DOM syntax for element referencing the input and populate it with the select objects selected options value
  document.getElementById("eStop").value = o.value;
} 

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.