0
votes

How can i take a value when a user choose an option from a drop down menu(select) and add it with another choice of another drop down menu and output the total of a mark? I want this total to change automatically with on-change method. I have wrote my code in JavaScript using DOM to write the form! Can you help me please?

2
You said you have written code, but you have not put any code in your question. - Alex W

2 Answers

0
votes

I would personally recommend doing this with AngularJs. If you are using Angular, simply assigning the DOM element with a model (ng-model) name and you can dynamically change the items inside of your dropdown in your javascript. Check out this very simple example:

http://plnkr.co/edit/?p=preview

and here is documentation on how to use the ng-change utility that comes with AngularJs:

http://docs.angularjs.org/api/ng.directive:ngChange

(edit: I guess I should describe what is happening in the example in more detail. Basically, what is inside of <div ng-controller="Controller"> will be tied to the controller called "Controller" in the script.js. By assigning ng-model="confirmed" to the checkbox, it ties a variable of the same name to the checkbox. As you can see a couple lines down, simply calling {{confirmed}} can call the value from html. Also, assigning ng-change="change()" to the element basically tells the controller to call the function, change(), whenever there is changes made to the element in question. Hope this helps)

0
votes

This is using regular javascript.

function Total()
{

var e1 = document.getElementById("ObjectsIDValue1");
var e2 = document.getElementById("ObjectsIDValue2");

if(e1.selectedIndex < 0||e2.selectedIndex < 0 )
   return;//nothing is selected in 1 of the dropdowns

var ValueUserSelected1= new Number(e1.options[e1.selectedIndex].value);   
var ValueUserSelected2= new Number(e2.options[e2.selectedIndex].value);

var e3 = document.getElementById("TotalsIDValue");

e3.value = ValueUserSelected1+ValueUserSelected2;//assuming it's an input:text
};

And just add this to your select objects.

<Select id = "ObjectsIDValue1" onchange="Total();">
    <option>1</option>
    <option>2</option>
    <option>3</option>
    <option>4</option>
   <!-- more option objects -->
</Select>


<Select id = "ObjectsIDValue2" onchange="Total();">
   <option>5</option>
   <option>6</option>
   <option>7</option>
   <option>8</option>
   <!-- more option objects -->
</Select>

Note: This doesn't validate whats in your select is a number.

Edit: As you appear to be new here on Stack Overflow I'd like to mention that if you found this solved your problem please mark it as the accepted answer by giving it a check mark. Also if you found this helpful and have enough points to up-vote please do that too.

Hope this helps.

Edit2: From the OP's comment I think you're looking for this,

document.getElementById("ObjectsIDValue1").onchange = function(){Total()};

document.getElementById("ObjectsIDValue2").onchange = function(){Total()};

just add that after you define Total();

Edit3: I want to be forthcoming and explain that what you're looking to do is VERY ugly in regular Javascript to do. I would recommend a JS library such as JQuery where doing this is much cleaner and easier to code.

Anyway here is ALL the code needed to do this.

<script type = "text/javascript">

//Create Selects from an Array
function CreateSelectFromArray (SelectID, ArrayOfValues)
{
    var Code="";
    Code += "<Select id='"+SelectID+"'>";
    for(var x = 0; x < ArrayOfValues.length; x++)
    {
        Code += "<option>";
        Code += ArrayOfValues[x];
        Code += "</option>";
    }
    Code += "</Select>";

    return  Code;
};

//Repopulate a select from a start and end value
function ChangeSelectFromValues (SelectID, StartValue,EndValue)
{
    var Code="";
    var SelectObject = document.getElementById(SelectID);
    for(var x = StartValue; x <= EndValue; x++)
    {
        Code += "<option>";
        Code += x;
        Code += "</option>";
    }
    SelectObject.innerHTML = Code;  
};

//Add up the values of 2 Selects (currently hardcoded)
function Total()
{

var e1 = document.getElementById("ObjectsIDValue1");
var e2 = document.getElementById("ObjectsIDValue2");

if(e1.selectedIndex < 0||e2.selectedIndex < 0 )
   return;//nothing is selected in 1 of the dropdowns

var ValueUserSelected1= new Number(e1.options[e1.selectedIndex].value);   
var ValueUserSelected2= new Number(e2.options[e2.selectedIndex].value);

var e3 = document.getElementById("TotalsIDValue");

e3.value = ValueUserSelected1+ValueUserSelected2;//assuming it's an input:text
};


var yourArray = [5,10,15];
var SelectCode="";

SelectCode += CreateSelectFromArray ('FirstSelect', yourArray);//create select with array values
SelectCode += CreateSelectFromArray ('SecondSelect', yourArray);//create select with array values
SelectCode += CreateSelectFromArray ('ObjectsIDValue1', [1,2,3,4,5]);//create select with default array
SelectCode += CreateSelectFromArray ('ObjectsIDValue2', [1,2,3,4,5]);//create select with default array
SelectCode += "<input id='TotalsIDValue'/>";

var doc = document.open("text/html","replace");
doc.writeln(SelectCode);
doc.close()

//Add events to the Selects for desired functionality
document.getElementById("FirstSelect").onchange = function(){
                                                    var e0 = document.getElementById("FirstSelect"); 
                                                    ChangeSelectFromValues('ObjectsIDValue1',1,new Number(e0.options[e0.selectedIndex].value));
                                                    };

document.getElementById("SecondSelect").onchange =  function(){
                                                    var e0 = document.getElementById("SecondSelect"); 
                                                    ChangeSelectFromValues('ObjectsIDValue2',1,new Number(e0.options[e0.selectedIndex].value));
                                                    };

document.getElementById("ObjectsIDValue1").onchange = function(){Total()};

document.getElementById("ObjectsIDValue2").onchange = function(){Total()};

</script>

That's about as clean as you can get this in regular JavaScript

Note:

var doc = document.open("text/html","replace");
doc.writeln(SelectCode);
doc.close()

Can be replace with,

document.getElementById("PlaceYouWantTheSelects").innerHTML =SelectCode; 

If you have a specific place in mind to put the selects.