0

How do I acces some object data in a javascript function? All I want to do is to take some input from the html file and then if the input text is === to one of my objects in javascript i want to acces some data from that object to use within my function.

For example:

I have the html form with 2 text inputs and a button to acces the function. And in the javascript document I have two objecst called bob and susan with the data "bob.age = 25" and "susan.age = 30". So I want a function that calculates bob.age + susan.age. But I want to use the inputs of bob and susan in my form html. So when i have the inputs bob and susan i want the function to do bob.age + susan.age

here is my html form:

<form name="mmForm">
       <label for="element1">E1</label>
       <input type="text" id="element1">
       <label for="element2">E2</label>
       <input type="text" id="element2">
       <input type="button" value="Calculate" onclick="procesForm_mm()">
       <div id="resultfield_mm">Result:</div>
   </form>

here my javascript function:

function procesForm_mm() {
var e1 = document.mmForm.element1.value;

var e2 = document.mmForm.element2.value;

result_mm = parseInt(e1) + parseInt(e2);

document.getElementById("resultfield_mm").innerHTML += result_mm;
}

and this is the data i want to acces:

var Fe = new Object();
Fe.denumire = "Fier";
Fe.A = 56;
Fe.Z = 26;
Fe.grupa = "VIIIB";
Fe.perioada = 4;
  • 4
    This question is unclear and maybe incomplete. – Denys Séguret Jul 31 '13 at 12:19
  • Your question is not clear and you need to be more precise on what you want to achieve. What code do you have so far? In terms of comparing values held in an object and those from a text field as long as the object is either global or is passed into the said function as a parameter you will be able to do your calculations / comparisons – Mark Walters Jul 31 '13 at 12:25
  • i just added my curent code. hope that you understand now. – Iulian Constantin Redinciuc Jul 31 '13 at 12:31

1 Answers1

0

Try this (a lot of guessing involved):

function procesForm_mm() {
    var e1 = document.mmForm.element1.value;
    var e2 = document.mmForm.element2.value;
    result_mm = parseInt(eval(e1).A) + parseInt(eval(e2).A);
    document.getElementById("resultfield_mm").innerHTML += result_mm;
}

var Fe = new Object();
Fe.denumire = "Fier";
Fe.A = 56;
Fe.Z = 26;
Fe.grupa = "VIIIB";
Fe.perioada = 4;

var Co = new Object();
Co.denumire = "Cobalt";
Co.A = 59;
Co.Z = 27;
Co.grupa = "IXB";
Fe.perioada = 4;

See it working here: http://jsfiddle.net/KJdMQ/.

It's important to keep in mind that use of the JS eval function has some disadvantages: https://stackoverflow.com/a/86580/674700.

A better approach would be to keep your JS objects in an array and avoid the use of the eval function:

function procesForm_mm() {
    var e1 = document.mmForm.element1.value;
    var e2 = document.mmForm.element2.value;
    result_mm = parseInt(tabelPeriodic[e1].A) + parseInt(tabelPeriodic[e2].A);
    document.getElementById("resultfield_mm").innerHTML += result_mm;
}
var tabelPeriodic = [];

tabelPeriodic["Fe"] = new Object();
tabelPeriodic["Co"] = new Object();

var el = tabelPeriodic["Fe"];
el.denumire = "Fier";
el.A = 56;
el.Z = 26;
el.grupa = "VIIIB";
el.perioada = 4;

el = tabelPeriodic["Co"];
el.denumire = "Cobalt";
el.A = 59;
el.Z = 27;
el.grupa = "IXB";
el.perioada = 4;

(See it working here)

Note: This looks like a chemistry application, I assumed that the form is supposed to add some chemical property values for the chemical elements (i.e. A possibly being the standard atomic weight). The form would take as input the names of the JS objects (Fe and Co).

Community
  • 1
  • 1
Alex Filipovici
  • 31,789
  • 6
  • 54
  • 78