[RESOLVED]Checkbox doesn't check

Why doesn’t the checkbox check? None of the 3 lines of code do it. Thanks.

<!doctype html>
<html lang="en">
<head>

</head>
<body>
<ul>
<li>foo</li>
<li>bar</li>
</ul>
<input type="checkbox" id = "Chk" class="test"/>
<div id="Who"  >Hi
</div>

      <script type ="text/javascript" src="http://code.jquery.com/jquery-latest.js">

    $(document).ready(function () {

            $(‘#Chk’).attr(‘checked’, ‘checked’);
            $(‘#Chk’).prop(‘checked’, true);
            $(‘#Chk’).prop(‘checked’) = true;

    });
</script>
</body>

</html>

You are missing a closing </script> tag for your jQuery reference :

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type='text/javascript'>
    $(function () {
        // Check your checkbox
        $('#Chk').prop('checked', true);
    });
</script>

You can see a working example of your code here.

try this

<!doctype html>
<html lang="en">
<head>
    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js">  </script>
    <script type="text/javascript">
        $(document).ready(function () {
            $('#Chk').prop('checked', 'checked');
        });
    </script>
</head>
<body>
    <ul>
        <li>foo</li>
        <li>bar</li>
    </ul>
    <input type="checkbox" id="Chk" class="test" />
    <div id="Who">
        Hi
    </div>
</body>
</html>

Thanks for the reply Rion… I successfully made the changes. Now I have one more question…

I’ve added a textbox and am trying to change the text property but it doesn’t work.

<!doctype html>
<html lang="en">
<head>

</head>
<body>
<ul>
<li>foo</li>
<li>bar</li>
</ul>
<input type="checkbox" id = "Chk" class="test"/>
<input type="text" id = "Textbox1" class="test2"/>
<div id="Who"  >Hi
</div>

      <script type ="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
      <script type = "text/javascript">
    $(document).ready(function () {

            $(‘#Chk’).prop(‘checked’, true);
            $(‘#Textbox1′).text("hello");

    });
</script>
</body>

</html>

Since you are using an actual <input> element, try using the .val() function instead of .text() :

$('#Textbox1').val("hello");

Rion, why wouldn’t .text() work for a  asp textbox?

<!doctype html>
<html lang="en">
<head>

</head>
<body>
<ul>
<li>foo</li>
<li>bar</li>
</ul>
<input type="checkbox" id = "Chk" class="test"/>
<input type="text" id = "Textbox1" class="test2"/>
<form runat="server">

<asp:TextBox ID="TextBox2" runat="server"  ClientIDMode="Static" CssClass="one"></asp:TextBox>

</form>

<div id="Who"  >Hi
</div>

      <script type ="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
      <script type = "text/javascript">
    $(document).ready(function () {

            $(‘#Chk’).prop(‘checked’, true);
            $("#TextBox2").text("hello");
 

    });
</script>
</body>

</html>

davelewicki

Rion, why wouldn’t .text() work for a  asp textbox?

It’s just the way that it was designed, as per
the documentation
:

The .text() method cannot be used on form inputs or scripts. To set or get the text value of input or textarea elements, use the .val() method. To get the value of a script element, use the .html() method.

Leave a Reply