I've tested the code below and confirmed that the loop executes the required number of times and the html looks well-formed, but it seems that it doesn't add the control the div listed.
$('"[ID^=numAnsMulChS]').blur(function() {
var tmpInt = $(this).val();
var tmpId = $(this).closest("div").attr("id");
//alert('Handler for .blur() called.' + tmpInt + '||' + tmpId);
for (i = 1; i <= parseInt(tmpInt); i++) {
//alert('<input type="radio" name="' + tmpId + 'Ans" id="' + tmpId + tmpInt + i + '" value="' + i + '">');
var radBtn = $('<input type="radio" name="' + tmpId + 'Ans" id="' + tmpId + tmpInt + '" value="' + i + '">');
alert(radBtn);
$(tmpId).append(radBtn);
//radBtn.appendTo($(tmpId));
}
});
I should add that the intent of this script is to take input from a textbox (numeric value) and then add that number of radio buttons....
set tmpID to the string value if the id (if it exists) as there is no prefix, it usesd as a tag name, not an id. an id selector should start with "#", so its $("#" + tmpId'), but as you just found the dom element, looking it up again by id, is just bad
coding. just:
var $tmp = $(this).closest('div');
...
$tmp.append(radBtn);
bruce (sqlwork.com)
Marked as answer by aploessl1 on May 25, 2012 02:44 AM
Thanks a bunch. I knew if I got another set of eyes looking at it, I'd find out at that I made a silly oversight. After incorporating your changes, I get the following which I've tested as functional:
$('[ID^=numAnsMulChS]').blur(function() {
var tmpInt = $(this).val();
var tmpId = $(this).closest("div").attr("id");
var $tmp = $(this).closest('div');
for (i = 1; i <= parseInt(tmpInt); i++) {
var radBtn = $('<input type="radio" name="' + tmpId + 'Ans" id="' + tmpId + tmpInt + '" value="' + i + '">');
$tmp.append(radBtn);
}
});
aploessl1
Member
616 Points
197 Posts
Any ideas on dynamically adding controls
May 24, 2012 10:09 PM|LINK
I've tested the code below and confirmed that the loop executes the required number of times and the html looks well-formed, but it seems that it doesn't add the control the div listed.
$('"[ID^=numAnsMulChS]').blur(function() { var tmpInt = $(this).val(); var tmpId = $(this).closest("div").attr("id"); //alert('Handler for .blur() called.' + tmpInt + '||' + tmpId); for (i = 1; i <= parseInt(tmpInt); i++) { //alert('<input type="radio" name="' + tmpId + 'Ans" id="' + tmpId + tmpInt + i + '" value="' + i + '">'); var radBtn = $('<input type="radio" name="' + tmpId + 'Ans" id="' + tmpId + tmpInt + '" value="' + i + '">'); alert(radBtn); $(tmpId).append(radBtn); //radBtn.appendTo($(tmpId)); } });I should add that the intent of this script is to take input from a textbox (numeric value) and then add that number of radio buttons....
bruce (sqlwo...
All-Star
36822 Points
5441 Posts
Re: Any ideas on dynamically adding controls
May 25, 2012 02:20 AM|LINK
please read the docs on jquery selectors.
var tmpId = $(this).closest("div").attr("id");set tmpID to the string value if the id (if it exists) as there is no prefix, it usesd as a tag name, not an id. an id selector should start with "#", so its $("#" + tmpId'), but as you just found the dom element, looking it up again by id, is just bad coding. just:
var $tmp = $(this).closest('div');
...
$tmp.append(radBtn);
aploessl1
Member
616 Points
197 Posts
Re: Any ideas on dynamically adding controls
May 25, 2012 02:44 AM|LINK
Thanks a bunch. I knew if I got another set of eyes looking at it, I'd find out at that I made a silly oversight. After incorporating your changes, I get the following which I've tested as functional:
$('[ID^=numAnsMulChS]').blur(function() { var tmpInt = $(this).val(); var tmpId = $(this).closest("div").attr("id"); var $tmp = $(this).closest('div'); for (i = 1; i <= parseInt(tmpInt); i++) { var radBtn = $('<input type="radio" name="' + tmpId + 'Ans" id="' + tmpId + tmpInt + '" value="' + i + '">'); $tmp.append(radBtn); } });Thanks again...