Link to home
Start Free TrialLog in
Avatar of brgdotnet
brgdotnetFlag for United States of America

asked on

How to single quote a check box id using java script before using document.GetElementById ?

I can check a checkbox as follows using Java Script :
document.getElementById('Checkbox2').checked = true;

It works because Checkbox2 is wrapped in single quotes. So how can I wrap single quotes around
the check box name when the name is passed into the function "startTheProcess"?
I tried something like this, but it does not work on line 4  :  checkTheBox( + "'" + CheckBoxId + "'" + );



1  function startTheProcess(CheckBoxId)
2 {
3  // If the value passed in for CheckBoxId is not single quoted. How do I single quote it?
4   checkTheBox(CheckBoxId);
5}


function checkTheBox(CheckBoxId)
{
    document.getElementById(CheckBoxId).checked = true;

 //  document.getElementById('Checkbox2').checked = true; // This works, but is hard coded
}
SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of brgdotnet

ASKER

Hi Ryan, the extra space is  typo, and yeah, in the signature of the function I forgot the second parameter. Anyway, I still cannot get it to work. On the java script
side I tried adding two single quotes to the variable passed in, but that does not work either?
the extra space is  typo, and yeah, in the signature of the function I forgot the second parameter. Anyway, I still cannot get it to work. On the java script
side I tried adding two single quotes to the variable passed in, but that does not work either?
yup, typo does affect results.

how's your codes look like now? did you clear the browser's cache before retry your page?
Well I didn't quite get the answer I wanted, but I think you guys brought me closer to a way think of it differently. Thank you.
You're welcome, glad to help.
Well I didn't quite get the answer I wanted
I thought Ryan had answered your question so I deleted my response which I assumed was a duplicate. I am reposting here in case it is useful.
The gist of it is that you are calling your show() with two parameters but it only takes one - because the checkbox id is the second parameter the show() function never sees it

Let's look at your code
string showPopup = "return show('" + e.Row.Cells[data.Row.Table.Columns["CustId"] + " ' , ' " + cbxBox.ID.ToString() + " ');";

Open in new window

This will render something like this
<input type="checkbox" onclick="return show('CustID001','Checkbox1')" ... />

Open in new window

show is defined as
function show(theckbox) {

Open in new window

Notice the difference in the parameters - your function expects 1 is being sent 2 so you are using the customerID to try and target the checkbox
Either change your C# code to only send one parameter
string showPopup = "return show('" + cbxBox.ID.ToString() + " ');";

Open in new window

Or change your function to accept the customerID - not sure why you would do this though as you don't seem to use it
function show(custID, theckbox) {

Open in new window