Click here to Skip to main content
15,888,286 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have written a code that will select the check box and display the text of selected checkbox without appending(time being).Java Script is not working... What is the error?

XML
<input id="Checkbox1" type="checkbox" />
       <input id="Checkbox2" type="checkbox" />
       <label id="checktext"></label>

The java script is :

checktext.innerText = gettext("Checkbox1", "Checkbox2");

C#
function gettext(a, b) {
    var x = document.getElementById(a);
    var y = document.getElementById(b);
    if (x.checked)
        return x.textContent;
    else if (y.checked)
        return y.textContent;
}
Posted
Comments
First of all, your Checkbox is not having any text contents.
So, how the code will work?
SubhamSoni123 19-Nov-13 21:48pm    
Even if I give a text content (text between <input> </input>.. it doesn't work)..
That is because input tags store text in their "value" attribute. Like...

<input id="Checkbox1" type="checkbox" value="Check1"/>

So, do you need this?

1 solution

SQL
You've got single-equals instead of double-equals in your if statements:

if (checkbox.checked=true) {

should be

if (checkbox.checked == true) {

or just

if (checkbox.checked) {
 
Share this answer
 
Comments
There is no equals in his code. He has used like if (x.checked) and it is correct.
SubhamSoni123 19-Nov-13 21:47pm    
Even if I give a text content (text between <input> </input>.. it doesn't work)..
Alexanbu 20-Nov-13 0:17am    
<form action="demo_form.asp">

Enter a date before 1980-01-01:
<input type="date" name="bday" max="1979-12-31">

Enter a date after 2000-01-01:
<input type="date" name="bday" min="2000-01-02">

Quantity (between 1 and 5):
<input type="number" name="quantity" min="1" max="5">

<input type="submit">

</form>

Syntax:
<input min="number|date">

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900