0

I need to pass checkbox Boolean value(True or False) from below java method to java script function.

Java Method::

M1() {
  generatedXML.append("<div id=checkboxes> ");
  generatedXML.append("<input type=checkbox ");
  generatedXML.append(" onchange=\"setValue('");
  generatedXML.append(obj);
  generatedXML.append("','");
  generatedXML.append('this');
  generatedXML.append("');\"");
}

JavaScript function::

function setValue(obj,refr) 
{
  alert(refr.checked); // i think it will alert true or false??
  //Need Boolean value of checkbox 
}

2 Answers 2

1

You should add an id attribute to the checkbox and in the javascript access the checkbox itself by its own id. Something like

document.getElementById('checkIdHere');
Sign up to request clarification or add additional context in comments.

Comments

0

Assign your checkbox with an ID and in onchange function access checkbox with that id or you can pass the instance of checkbox to onchange function and directly access the value of checkbox.

var value = document.getElementById("chkTest").checked

And if you want to access value without id than pass this to onchange function from java like (syntax may not be correct please ignore as i don have editor for this)

generatedXML.append(" onchange=\"setValue('");
generatedXML.append(obj);
generatedXML.append("','");
generatedXML.append(path);
generatedXML.append("',this);
generatedXML.append(");\"");

And in javascript do this

function setValue(obj,path,chkBox) {
var value = chkBox.checked;
}

9 Comments

Thanks for your answer. but i am facing syntax problem i.e how to pass 'this' from this method. i am trying to pass like generatedXML.append(this); , but in javascript i am getting undefined value.
have you changed the javascript function as well ?
//generatedXML.append(this); function setValue(obj,path,refr) { alert(refr.checked); // i think it will alert true or false }
Ok may be this is being used of java try to surround it in single qoutes
you mean like this? generatedXML.append('this');
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.