3

Hello People here is my code,

function send()
{
  var param_count=document.getElementsByName('eqt_param[]');

  for (var i=0; i<param_count.length; i++)
  {
    var test=param_count[i].value;  
    var param_value='Eqt_Param'+i+'='+test;

    alert(param_value);
  }
}

if i alert i get "Eqt_Param0=4.00" then "Eqt_Param1=3.00" but i want to alert at once output should be something like "Eqt_Param0=4.00,Eqt_Param1=3.00 " after alerting this way i also want to remove the ',' in between how to fix this?

2
  • 1
    put your html code part. it will be useful Commented Nov 28, 2012 at 9:45
  • Not quite sure what your problem is. There is no array in your code... do you want to use one? If yes, where are you stuck? I recommend to read the MDN documentation to learn the basics about arrays. Commented Nov 28, 2012 at 9:45

4 Answers 4

3

Do you mean this:

function send()
{
  var param_count=document.getElementsByName('eqt_param[]');
  var values = [];
  for (var i=0; i<param_count.length; i++)
  {
    values.push('Eqt_Param'+i+'='+param_count[i].value)
  }
  alert(values.join(', '));
}
Sign up to request clarification or add additional context in comments.

1 Comment

I edited the code: Use literals instead of objects. Variables test and param_value are unnecessary.
0

Array has some useful functions to make your life easier, forEach and join.

function send()
{
  var toPrint = []
  document.getElementsByName('eqt_param[]').forEach( function(el, idx) {
    toPrint.append('Eqt_Param'+idx+'='+x.value);
  }
  alert(toPrint.join(', '));
}

Comments

0
function send()
{
  var tempArray=[]; 
  var param_count=document.getElementsByName('eqt_param[]');

  for (var i=0; i<param_count.length; i++)
  {
     var test=param_count[i].value;  
     var param_value +='Eqt_Param'+i+'='+test;
     tempArray.push(param_value)

  }

    alert(tempArray.join(','));  //to join with ','
    var joinedstr=tempArray.join(',');
    var finalArray= joinedstr.split(',');  //to split with ','
}

Comments

0

You do not need an array for that:

alert((function(array, string, _i, _len) {
    for(; _i < _len; _i++)
        string += 'Eqt_Param' + _i + ' = '+ array[_i].value + ", ";
    return string.substr(0, string.length -2);
    } (document.getElementsByName('eqt_param[]'), "", 0, document.getElementsByName('eqt_param[]').length)));

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.