I just want to blog this particular programming feat to archive this specific issue and to help other programmer that would be facing the same problem.

The problem is, I have a dynamic textbox with a certain value from a database. I am using coldfusion for this particular project so if your using other scripts (php, asp, jsp, etc) then it will be fine since the main course of this post is the javascript side as mentioned in this article’s title, and that is “Creating an array from multiple textboxes value using javascript”.

First, fetching my database:

<cfquery name=”qvalidate” datasource=”#db1#”>
SELECT id_user
FROM tbl_editor;
</cfquery>

Then creating my dynamic textbox by looping the result:

<cfset count=”1″>
<cfloop list=”#qvalidate.id_user#” index=”i”>
<cfinput type=”text” name=”txteditor#count#” value=”#i#”>
<cfset count=#count#+1>
</cfloop>

Now, what I need is to have the value of those pre-populated textboxes and put it on an array. And before you’d ask on why I didn’t just did <cfset myarray=#quotedvaluelist(qvalidate.id_user)#> and then put it on a textbox and then fetch the value using javascript and stored in a variable so i’ll have something like this: myarray=frmmain.txtbox.value. I tried this but this doesn’t seem to work so I came up with this solution.

First use this code I googled around (this code seems to appear on almost all site and I can’t remember where did I got it so giving credit is not possible, and this seems like a public property anyway but keywordspeak.com wants to thank whoever the original source is).

Array.prototype.inArray = function (value)
{

var i;
for (i=0; i < this.length; i++)
{
if (this[i] == value)
{
return true;
}
}
return false;
};

Then here comes my custom code:

for (i=1; i<counter; i++)
{
var txtname=’txteditor’+i;
var editor_value=frmeaccess.elements[txtname].value
editors_array[i]=editor_value
}

if (editors_array.inArray(myvaluetotest))
{

//value is in the array
}

Now, there’s all of it, for my future reference and for those who’s facing the same problem. comments are welcome.

Related posts:

  1. Javascript – Refreshing Grand Parent Window