Web Analytics Made Easy -
StatCounter Jquery and a Checkbox - CodingForum

Announcement

Collapse
No announcement yet.

Jquery and a Checkbox

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Jquery and a Checkbox

    Hey guys I am trying to do something very simple but its not works as i want it to.

    What I want to do is if the url in the browser mach http://www.MYSITE.com/live/category/0?live then I want to have jquery activate the checkbox with the id of only_live. currently this script is on a loop for some reason. please help me.

    Here is my html

    Code:
    <td>
    <input type="checkbox" id="only_live" name="only_live" onclick="this.form.submit();" value="true" />
    </td>
    <td>&nbsp; SOME TEXT</td>
    Here is my Jquery
    Code:
    <script type="text/javascript">
    $(function()
    {
    if(window.location == "http://www.MYSITE.com/live/category/0?live")
    {
    if($('#only_live').is(':checked'==false))
    {
    $('input[name=only_live]').attr('checked', true);
    }
    }
    });
    </script>
    Last edited by Atrhick; Aug 18, 2011, 03:30 PM.

  • #2
    This line is the main problem
    Code:
    if($('#only_live').is(':checked'==false)) {
    I think this is what you really wanted to do (jQuery 1.6 or higher)
    Code:
    if($('#only_live').prop('checked')==false)) {
    Prior to jQuery 1.6 you can check .attr() instead. But be careful as the attr is "undefined" before the checkbox has actually been changed
    Code:
    if($('#only_live').attr('checked')==false)) {

    Comment


    • #3
      you could also do

      Code:
      if($('#only_live').prop('checked','false')) {
      Last edited by DanInMa; Aug 19, 2011, 10:31 AM. Reason: removed extra parentheses
      - Firebug is a web developers best friend! - Learn it, Love it, use it!
      - Validate your code! - JQ/JS troubleshooting
      - Using jQuery with Other Libraries - Jslint for Jquery/other JS library users

      Comment

      Working...
      X