2

Could you please tell me how to make from this 4 functions just 1. Cause they all do one thing, with just one parameter changing all the time. Also, I need to take the value of each time the function is running and put it into a new variable so I can after calculate it.

function getValue(age)
{
    for (var i = 0; i < document.getElementsByName('age').length; i++)
    {
        if (document.getElementsByName('age')[i].checked)
        {
            return document.getElementsByName('age')[i].value;
        }
    }
}


function getBmiValue()
{
    for (var i = 0; i < document.getElementsByName('bmi').length; i++)
    {
        if (document.getElementsByName('bmi')[i].checked)
        {
            return document.getElementsByName('bmi')[i].value;
        }
    }
}    

function getFamValue()
{
 for (var i = 0; i < document.getElementsByName('fam').length; i++)
 {
    if (document.getElementsByName('fam')[i].checked)
        {
            return document.getElementsByName('fam')[i].value;
        }
    }   
 }

function getDietValue()
{
 for (var i = 0; i < document.getElementsByName('diet').length; i++)
  {
      if (document.getElementsByName('diet')[i].checked)
       {
            return document.getElementsByName('diet')[i].value;
       }
    }     
}
1
  • Pass the element name as a parameter to your function. You'll need to have a basic understanding of the language syntax, so I'd recommend learning a bit more JavaScript before going too much further. Commented Jul 9, 2014 at 15:03

3 Answers 3

2
function getValueByElementName(element_name)
{
 for (var i = 0; i < document.getElementsByName(element_name).length; i++)
 {
    if (document.getElementsByName(element_name)[i].checked)
        {
            return document.getElementsByName(element_name)[i].value;
        }
    }   
 }

Or a little bit optimization:

function getValueByElementName(element_name)
{
   var elements = document.getElementsByName(element_name);

   for (var i = 0; i < elements.length; i++)
   {
      if (elements[i].checked)
            return elements[i].value;
    }   
 }
Sign up to request clarification or add additional context in comments.

Comments

1
function getValue(tagname)
{
    for (var i = 0; i < document.getElementsByName(tagname).length; i++)
    {
        if (document.getElementsByName(tagname)[i].checked)
        {
            return document.getElementsByName(tagname)[i].value;
        }
    }
}

Pass your element name as a variable in this function and call it.

Comments

0

Here is an example of what you can do:

function getValue(key) {

// get this once, not on each loop iteration  
var elem = document.getElementsByName(key);

  // cache the length too, so not to calculate it on each loop iteration
  for (var i = 0, len = elem.length; i < len; i++) {

    if (elem[i].checked) {
      return elem[i].value; 
      // this will exit the function when it finds the FIRST one. 
      // Is that what you want?
    }
  }
}

// you can call above function like this:
getValue('age');
getValue('bmi');
getValue('fam');
getValue('diet');

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.