javascript - How to get ClientID in a variable -
i have 7 check boxes 7 weeks , separate time picker 7 days on page. when check box checked want show time picker week day, , if unchecked want hide respective time picker
i have used following code this, , working too.
function chk_weekday_clicked(ctrl, week) { if (ctrl.checked) { if (week == "monday") document.getelementbyid("<%= tm_monday_from.clientid %>").style.display = ""; else if (week == "tuesday") document.getelementbyid("<%= tm_tuesday_from.clientid %>").style.display = ""; else if (week == "wednesday") document.getelementbyid("<%= tm_wednesday_from.clientid %>").style.display = ""; //rest of days } else{ if (week == "monday") document.getelementbyid("<%= tm_monday_from.clientid %>").style.display = "none"; else if (week == "tuesday") document.getelementbyid("<%= tm_tuesday_from.clientid %>").style.display = "none"; else if (week == "wednesday") document.getelementbyid("<%= tm_wednesday_from.clientid %>").style.display = "none"; //rest of days } }
chk_weekday_clicked event handler week day check box. first parameter clicked checkbox control , second parameter name of week day.
all time picker control's name have same naming patterned as, tm_[weekname]_from
so, question instead of using these many if-else can in couple of lines? code below.
function chk_weekday_clicked(ctrl, week) { //i want controlid using parameter. , must clientid using on page having masterpage. var controlid = "tm_" + week + "_from"; if (ctrl.checked) { document.getelementbyid(controlid).style.display = ""; } else { document.getelementbyid(controlid).style.display = "none"; } }
my actual problem how can control's clientid in variable. page using master page, must use clientid controls id.
so, can suggest solution this
clientid generated asp.net, can't created using javascript variable. can this
function chk_weekday_clicked(ctrl, week) { //since client id generated asp.net //create list of ids var controls = { "monday" : "<%= tm_monday_from.clientid %>", "tuesday" : "<%= tm_tuesday_from.clientid %>", "wednesday" : "<%= tm_wednesday_from.clientid %>", } //fetch control id var controlid = controls[week.tolowercase()]; //desired operation if (ctrl.checked) { document.getelementbyid(controlid).style.display = ""; } else{ document.getelementbyid(controlid).style.display = "none"; } }
Comments
Post a Comment