1

I've a page with two formulas one. Each one has it's own form with inputs etc. They both call the same Javascript function on key up.

Only the first one works, I kind of understand why but I can't find a resolution, I'm too new to Javascript to know how to tackle the problem. I can't change the structure of the JS file a great deal as other equations on other pages depend on this set up.

Is there a workaround?

Shortened HTML:

 <div id="formula">

<p>To find &rho;<sub>b</sub>:</p>
        <form id="formula" name="formula">

            <input type="hidden" id="formulaName" name="formulaName" value="porosityRhob"/>

            <div>
                <label>$\rho_{fl}$:</label>
               <input type ="text" name="input" id="input" onkeyup="calculatePEFormula()"/>
            </div>
            <div>
                <label>Result:</label>
                <input type="text" id="result" name="result">
            </div>

        </form>
    </div>

    <br/>
    <div id="formula">

        <p>To find &Phi;:</p>
        <form id="formula" name="formula">

            <input type="hidden" id="formulaName" name="formulaName" value="porosityPhi"/>

            <div>
                <label>$\rho_{ma}$:</label>
                <input type ="text" name="input" id="input" onkeyup="calculatePEFormula()"/>
            </div>
            <div>
                <label>Result:</label>
                <input type="text" id="result" name="result">
            </div>

        </form>
    </div>

THE JS:

function PEFormula(result, formulaName){
this.result = result;
this.formulaName = formulaName;
}

function calculatePEFormula(){

var PEObject = new PEFormula($("#result"), $("#formulaName"));
var formulaName = $("#formulaName").val();

switch(formulaName){

    case "porosityRhob" : PEObject.porosityRhoB();
        break;
    case "porosityPhi" : PEObject.porosityPhi();
        break;
}
PEFormula.prototype.porosityPhi = function(){

var input = parseFloat($("#input").val());
//logic
return r;
}

4 Answers 4

1

The HTML attribute id is supposed to be unique

  1. <div id="formula">
  2. <form id="formula" name="formula">
  3. <input type="hidden" id="formulaName" name="formulaName" value="porosityRhob"/>
  4. <input type ="text" name="input" id="input" onkeyup="calculatePEFormula()"/>
  5. <input type="text" id="result" name="result">

Try changing these ids instead use classes

Here is the solution change all field ids to class and form id should be different and pass parameter id of form as parameter onkeyup="calculatePEFormula('form#formula1')" and onkeyup="calculatePEFormula('form#formula2')" Now in js

function PEFormula(result, formulaName){
this.result = result;
this.formulaName = formulaName;
}

function calculatePEFormula(form_id){

var PEObject = new PEFormula($(form_id+"  .result"), $(form_id+"  .formulaName"));
var formulaName = $(form_id+"  .formulaName").val();

switch(formulaName){

    case "porosityRhob" : PEObject.porosityRhoB();
        break;
    case "porosityPhi" : PEObject.porosityPhi();
        break;
}
PEFormula.prototype.porosityPhi = function(){

var input = parseFloat($(form_id+"  .input").val()); //provide the form id here
//logic
return r;
}
Sign up to request clarification or add additional context in comments.

Comments

0

You're using ID's when you should be using classes.

Comments

0

You need to avoid assigning the same id attribute to multiple elements. An element's ID should be unique.

Comments

0

Never use the same id for multiple elements. That's a basic concept in HTML. An identification should be unique and identical. So there cannot be any duplicated IDs inside any HTML page. So please change the ids of 2nd form,

<div id="formula2">

        <p>To find &Phi;:</p>
        <form id="formula2" name="formula">

            <input type="hidden" id="formulaName2" name="formulaName" value="porosityPhi"/>

            <div>
                <label>$\rho_{ma}$:</label>
                <input type ="text" name="input" id="input2" onkeyup="calculatePEFormula()"/>
            </div>
            <div>
                <label>Result:</label>
                <input type="text" id="result2" name="result">
            </div>

        </form>
    </div>

Change the IDs accordingly when calling the same js function.

I have renamed all the IDs by adding 2 at the end of each ID. Make every ID unique.

Comments

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.