Solved

(Probably easy) Help with javascript

Posted on 2014-11-29
3
197 Views
Last Modified: 2014-11-29
I have a script that works great but I have a slight problem. I've added a new window.onload script outside of the one below, so now only the last one works. How can I run this code without using widow.onload?

<script type="text/javascript">
        window.onload = function calcPer() {
                document.getElementById("lbOverage").onchange = function calcPer() {
                document.getElementById("lblPercentOveralge").innerHTML = this.options[this.selectedIndex].text + '%';
                document.getElementById("lblOverageQty").innerHTML = Math.ceil((this.options[this.selectedIndex].text / 100) * document.getElementById("lblFinalOrderQuantity").innerHTML)
                var x = +document.getElementById("lblFinalOrderQuantity").innerHTML;
                var y = +document.getElementById("lblOverageQty").innerHTML;
                document.getElementById("lblActualProductionQuantity").innerHTML = x + y;
                document.getElementById("lblOverageQty").innerHTML = '(+' + document.getElementById("lblOverageQty").innerHTML + ')'
            }
        }
    </script>

Open in new window


<asp:ListBox ID="lbOverage" runat="server" CssClass="alwaysontop" onChange="calcPer();" Height="85px"

Open in new window


I have tried this:

<script type="text/javascript">
        function calcPer() {
                document.getElementById("lbOverage").onchange = function calcPer() {
                document.getElementById("lblPercentOveralge").innerHTML = this.options[this.selectedIndex].text + '%';
                document.getElementById("lblOverageQty").innerHTML = Math.ceil((this.options[this.selectedIndex].text / 100) * document.getElementById("lblFinalOrderQuantity").innerHTML)
                var x = +document.getElementById("lblFinalOrderQuantity").innerHTML;
                var y = +document.getElementById("lblOverageQty").innerHTML;
                document.getElementById("lblActualProductionQuantity").innerHTML = x + y;
                document.getElementById("lblOverageQty").innerHTML = '(+' + document.getElementById("lblOverageQty").innerHTML + ')'
            }
        }
    </script>

Open in new window


which works, however it does not work on the first selection. When a user makes a list box selection, it does nothing. Once they make a second selection, it runs as expected.
0
Comment
Question by:Mike Miller
  • 2
3 Comments
 
LVL 43

Expert Comment

by:Rob
ID: 40472028
You're defining the onchange twice.  You only need to do it in the ListBox:
I've removed it from the code below
<script type="text/javascript">
function calcPer() {
	document.getElementById("lblPercentOveralge").innerHTML = this.options[this.selectedIndex].text + '%';
	document.getElementById("lblOverageQty").innerHTML = Math.ceil((this.options[this.selectedIndex].text / 100) * document.getElementById("lblFinalOrderQuantity").innerHTML);
	var x = +document.getElementById("lblFinalOrderQuantity").innerHTML;
	var y = +document.getElementById("lblOverageQty").innerHTML;
	document.getElementById("lblActualProductionQuantity").innerHTML = x + y;
	document.getElementById("lblOverageQty").innerHTML = '(+' + document.getElementById("lblOverageQty").innerHTML + ')';
}

    </script>

Open in new window

0
 
LVL 43

Accepted Solution

by:
Rob earned 500 total points
ID: 40472034
Demo here; http://jsbin.com/cutodo/1/edit?html,js,console,output

javascript
function calcPer() {
	var me = document.getElementById('lbOverage');
	document.getElementById("lblPercentOveralge").innerHTML = me.options[me.selectedIndex].text + '%';
	document.getElementById("lblOverageQty").innerHTML = Math.ceil((me.options[me.selectedIndex].text / 100) * document.getElementById("lblFinalOrderQuantity").innerHTML);
	var x = +document.getElementById("lblFinalOrderQuantity").innerHTML;
	var y = +document.getElementById("lblOverageQty").innerHTML;
	document.getElementById("lblActualProductionQuantity").innerHTML = x + y;
	document.getElementById("lblOverageQty").innerHTML = '(+' + document.getElementById("lblOverageQty").innerHTML + ')';
}

Open in new window


rendered html used:
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
	<div id='lblPercentOveralge'></div>
	<div id='lblOverageQty'></div>
	<div id='lblFinalOrderQuantity'>5</div>
	<div id='lblOverageQty'>3</div>
	<div id='lblActualProductionQuantity'></div>
<select id="lbOverage" onchange="calcPer()">
	<option value="1">1</option>
	<option value="2">2</option>
	<option value="3">3</option>
	<option value="4">4</option>
	<option value="5">5</option>
	
	</select>

</body>
</html>

Open in new window

0
 

Author Closing Comment

by:Mike Miller
ID: 40472037
Works like a charm. Nice catch. Thanks Rob!
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article discusses four methods for overlaying images in a container on a web page
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question