Dynamic HTML table with Javascript checkboxes puzzle - how to access checkbox.checked from outside of the function? -
before begin, should mention using javascript i'm not using jquery.
i have function obtains data site , displays in html table.
i add checkbox each row, , find if each 1 has been checked or not later (i.e. when button clicked), outside of function creates table.
the reason function makes table runs once , can't check if box checked or not (the user hasn't had chance check yet!).
each checkbox relates other data displayed on same row, can access outside of _cb_finditemsadvanced(root) declaring 'items' variable before function begins. however, can't seem checkboxes.
i can add normal checkboxes table with:
"<input type=checkbox...". however, can't seem access them outside of function makes table (and calls data). i've tried:
document.form1.sharedcheckboxname
it didn't seem work.
i have tried everything, creating global checkbox array , trying specify
" + checkbox[i] + " instead of
"<input type=checkbox...", but didn't work. know current code has specified variable of type checkbox, rather want populate table existing, global, array of checkboxes.
any appreciated, lost! hope you're not, after reading that! :-)
here code:
var items; var checkbox = []; function _cb_finditemsadvanced(root) { items = root.finditemsadvancedresponse[0].searchresult[0].item || []; var html = []; html.push('<table width="100%" border="0" cellspacing="0" cellpadding="3"><form name="form1"><tbody>'); (var = 0; < items.length; ++i) { var item = items[i]; var title = item.title; var pic = item.galleryurl; var viewitem = item.viewitemurl; checkbox[i] = document.createelement('input'); checkbox[i].type = "checkbox"; checkbox[i].name = "name"; checkbox[i].value = "value"; checkbox[i].id = "id" + i; if (null != title && null != viewitem) { html.push('<tr><td>' + '<img src="' + pic + '" border="0">' + '</td>' + '<td><a href="' + viewitem + '" target="_blank">' + title + '</a></td>' + '<td> <input type = "' + checkbox[i].type + '" + </t></tr>'); } } html.push('</tbody></table>'); document.getelementbyid("results").innerhtml = html.join(""); } if (checkbox[0].checked) { alert("hi"); //but nothing happens }
since using html string forget checkboxes array.
your form should wrap table not other way around. give form id
<form id="form1"> get form , find checkboxes
function findcheckeditems() { var checkeditems = []; var form = getelementbyid('form1'); var inputs = form.getelementsbytagname("input"); var checkboxindex = 0; (var = 0; < inputs.length; i++) { if (inputs[i].type.tolowercase() === 'checkbox') { if (inputs[i].checked) { var data = item[checkboxindex]; checkeditems.push(data); // whatever data } checkboxindex++; } } return checkeditems; }
Comments
Post a Comment