2

I made 2 input fields and 1 select field and I applied onchange() function to select tag which calls javascript and that script make calculation and show it in other two fields but it is not working for some syntax or logic reasons. please take a look at my code ,any help would be appreciated.

<html>
<head>
<script type="text/javascript">
function update() {
var x = document.getElementsByName("n_person").value;
document.getElementsByName("m_income").value= x*5;
document.getElementsByName("y_income").value= x*4;
}
</script>
</head>

<body>


<div class="elist"> <span class="b_text"><span>*</span>Level 1:</span>
// here is select tag where I put onchage function  <select class="ifield" name="n_person" onChange="update()">
    <option value="" selected="selected">Choose no. of person referred</option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
  </select>
// These are teh input where resultant value will appear  <input type="text" value="" placeholder="Your weekly Income..." name="m_income" id="weekly_income" class="field" readonly required />
  <input type="text" value="" placeholder="Your day Income..." name="y_income" id="day_income" class="field" readonly required/>
</div>
<!--elist-->

</body>
</html>
3
  • 2
    getElementsByName[0].value or use querySelector Commented Dec 27, 2015 at 14:36
  • Whenever you see getElements.. with an "s" it means plural, as in it returns a nodeList, not a single element Commented Dec 27, 2015 at 14:42
  • Thanks!! @tushar and Adeneo for pointing out mistake and explanation. Commented Dec 27, 2015 at 18:33

2 Answers 2

4

See this fiddle

Updated JS

function update() {
  var x = document.getElementsByName("n_person")[0].value;
  document.getElementsByName("m_income")[0].value = x * 5;
  document.getElementsByName("y_income")[0].value = x * 4;
}

The problem with your JS was you was not targetting the correct HTML elements using getElementsByName.

Please read more about it here

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

2 Comments

Thanks!! mate , for fixing the code :) query solved !
Great...glad that it helped.. :)
3

The method getElementsByName returns, as its name indicates, a list of elements with the specified name and not just one. In your case, the names are unique to the document and the method will return a list with just one value, but you'll still need to index this list. Therefore, you must change this:

var x = document.getElementsByName("n_person").value;

to

var x = document.getElementsByName("n_person")[0].value;

Do this also for the other uses of getElementsByName and your code will work.

1 Comment

Thanks!! Alex, you are right , I wasn't using "index" while assigning select options to variable x.

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.