0

This works, does anyone see anything I shouldn't be doing?

My function that is called

function getWeight(){
        var weight;
        var quantity = document.dhform.quantity.value;
        var cardSize = document.dhform.cardSize.value;
            weight = quantity * cardSize; 
        document.rates.weight.value = weight;
    }

takes values from these drop down menues

<td><span class="style29">Quantity</span><span class="style1"><br/>
                                <select id="Quantity" name="quantity" size="1">
                                    <option value="250">250</option>
                                    <option value="500">500</option>
                                    <option value="1000" selected>1000</option>
                                    <option value="1500">1500</option>
                                    <option value="2000">2000</option>
                                    <option value="2500">2500</option>
                                    <option value="3000">3000</option>
                                    <option value="4000">4000</option>
                                    <option value="5000">5000</option>
                                </select>
                            </span></td>
<td><p><span class="style1"><span class="style29">Size</span><br/>
                                      <select id="Size" name="Size" size="1" onChange="getWeight()">
                                        <option value="0.00999" selected>8.5 x 3.5</option>
                                        <option value="0.0146">11 x 4</option>
                                      </select>
                            </span></p></td>

Value needs to be inserted into this text box

<td style="width: 115px; height: 49px;"><span class="style16">Weight</span><br/>
                                  <input type="text" id="weight" name="weight" size="10" maxlength="4"/>
                              </td>
4
  • This is too general and sounds like "How do I use javascript?". Please be more specific. Commented Apr 30, 2010 at 14:58
  • you need ajax i think, you can google about it or be more specific to your question with sample code you are using. Commented Apr 30, 2010 at 14:59
  • Ajax may well not be necessary; where Ajax comes in is if the determination of the updated value requires going back to the server to fetch data. In many cases it doesn't. Commented Apr 30, 2010 at 15:01
  • Not easy to explain myself anymore than I did when I do not even know what I am lookng for. I am not looking for an answer just a point in a direction Commented Apr 30, 2010 at 15:10

1 Answer 1

2

Yes, it's done with Javascript. Let's say the "choose something" part is a drop-down (an HTML select box). You add an "onchange" event handler to that select box which fires a javascript function (which will automatically get the changed select box element as a parameter). Within that function, you use the value of the select box to determine what you want the value of the other box to be, and you update that other box's value.

Example:

<head>
<script language="JavaScript">
    function setToy(dropDown) {
        var pet = dropDown.options[dropDown.selectedIndex].value);
        var newBox = document.getElementById("toy");
        var toyText = "";
        switch(pet) {
           case "dog": toyText = "bone";
           case "cat": toyText = "mouse";
           default:toyText = "";
        }
        newBox.innerHTML = toyText;
    } 
</script></head>
<body>
<select name="petDropDown" onChange="updateToy(this)">
    <option value="dog">Dog</option>
    <option value="cat">Cat</option>
</select><br />
Preferred Toy: <input id="toy" />
</body>

I'll add that if you do this stuff a lot, you should look into jQuery, which makes this kind of thing much easier.

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

2 Comments

is newBox.innerHTML pointing towards the box I want to display it from?
The idea is that newBox is the input box marked "Preferred Toy" -- it's the input box you want to update as a result of the change. InnerHTML allows you to replace the contents -- basically it replaces everything between the <input id="toy"> and </input> tags.

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.