0
function something(frm,i){
//var ch=frm.outof1.value;
for(var j=1;j<=i;j++)
{
    //var b="outof" + j;
    alert(frm.outof+j.value);

}
//alert("outof" + i);
return false;
}

 $js='onClick="something(this.form,\''. $ii .'\')"';
  echo form_button('mybutton', 'Click Me', $js);

and getting output NAN where in html this is // echo form_input('outof'.$i,''); // the form input.

3
  • 3
    I think this may belong in the cryptography forum. Commented Apr 30, 2012 at 20:40
  • i need value of form input named outof1,outof2...etc...but i am getting output NaN Commented Apr 30, 2012 at 20:43
  • Can you post your rendered HTML here that browser get by this echo form_button('mybutton', 'Click Me', $js); ? Commented Apr 30, 2012 at 20:49

2 Answers 2

1

First, you will want to make sure the passed $ii is made into the correct type (not a string) by using parseInt. Then you construct the form input name by concatenating 'outof' and the number before evaluating .value.

function something(frm, i)
{
    for(var j = 1; j <= parseInt(i); ++j) {
        alert(frm['outof' + j].value);
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

+1, however i should expect an integer versus having to convert it within the method. Probably unquote that second arg. Agree?
@MattLo I normally pass verbatim variables to JavaScript using json_encode to make sure the intended type is passed without causing a parse error if the type was anything else :)
0
alert(parseInt(frm.outof) + parseInt(j.value))

Most likely, you're trying to sum up strings, and not integers

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.