views:

38

answers:

2

I'm trying to use the following loop to loop through dynamically created controls on my web form:

            for(x = 0; x <= count; x++) {
                Stmt += $("#DDLColumns" + x).val();
                switch($("#DDLConditional" + x).val()) {
                    case "is equal": Stmt += " = ";
                        break;
                    case "begins with": Stmt += " LIKE '%";
                        break;
                    };
                Stmt += $("#WhereText" + x).val();
                Stmt += ", ";
            }

and this is producing undefined and nulls as output from the val() functions. What am I doing wrong here?

A: 

Make sure you have a var Stmt outside of the for loop to avoid scoping issues and return Stmt at the end of the function.

Detect
you guys are correct to ask for the html markup that was where the problem was I didn't have an id for the elements in question just a name attribute.
Lyle
A: 

You may want to consider using different selectors to grab all of the DDLColumns with the prefix selector.

Depending on what kind of elements DDLConditional is selecting you may be able to use the same style selector there but using the value attribute.

Pseudo code:

$('[id|=DDLColumns').each( function(i,value){
   $(value).find('[value|=is]').each( function(){
       Stmt += " = ";
   });

   $(value).find('[value|=begins]').each( function(){
       Stmt += " LIKE ";
   });

   // more stuff
});
Zac