2

Okay so I'm using the SUPERSIMPLE code to check my checkboxes. I have an invisible ahref over my elements and a checkbox next to them. Here's the html:

<a href="#" class="transparent-checkbox-thingy" id="<?php echo $id;?>" onclick="check(<?php echo $id;?>)"></a>
<input type="checkbox" value="<?php echo $id;?>" name="users[]" class="newconvo-check" id="<?php echo $id;?>">

So I've tried using this method, where my check() function and input tags have the same id, using the following JQ:

function check(id)
{
    var newid = '#'+id;
    $(newid).prop('checked', true);
};

or this one

function check(id)
{

    $('#' + id).prop('checked', true);
};

or this one

function check(id)
{
    $(id).prop('checked', true);
};

None of them work. They return valid info on alert(id) or alert(newid) but they don't check the damned checkbox. So I tried this one:

$(document).ready(function() {
        $("a.transparent-checkbox-thingy").click(function(event) {
            event.preventDefault();
            var thisid = ('#' + event.target.id);
            $(thisid).prop('checked', true);
        });
    });

Didn't work. So I tried this

$(document).ready(function() {
        $("a.transparent-checkbox-thingy").click(function(event) {
            event.preventDefault();
            $('#1').prop('checked', true);
        });
    });

Didn't work. Yes, there is <input type="checkbox"> with id="1" always. And that's the one I'm testing. And yes, I have this

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
2
  • where is your script located? Above or below the jQuery you posted? Commented Feb 6, 2014 at 5:05
  • Below the src. Got it fixed, but thanks! Commented Feb 6, 2014 at 5:08

5 Answers 5

3

You have used same id in <a> and <input type="checkbox", id is unique you cant able use it as multiple place

 <a href="#" class="transparent-checkbox-thingy" id="<?php echo $id;?>" onclick="check(<?php echo $id;?>)"></a>
 <input type="checkbox" value="<?php echo $id;?>" name="users[]" class="newconvo-check" id="<?php echo $id;?>">

Use this,

<a href="#" class="transparent-checkbox-thingy" id="<?php echo $id;?>" onclick="check(<?php echo $id;?>)">Check</a>
<input type="checkbox" value="<?php echo $id;?>" name="users[]" class="newconvo-check" id="c_<?php echo $id;?>">

<script type="text/javascript">

function check(id)
{
    var newid = '#c_'+id;
    $(newid).prop('checked', true);
};

</script>
Sign up to request clarification or add additional context in comments.

2 Comments

Oh wow that's braindead of me.. Let me fix this and post working version >.< thanks
Thank you but since I'm passing it via the onclick(id) I don't actually need that id on the <a href.. I was just using it to figure out which checkbox should I tick. Since the value is passed to the fucntion, the id is no longer necessary!
1

First thing, id is unique, you can't use the same id in two places.

without id, instead you can use the selector as $('input[type="checkbox"]') like this

$('input[type="checkbox"]').prop('checked',true);

or you can use class like this

$('.newconvo-check').prop('checked',true);

JSFiddle

1 Comment

Should use .prop, not .attr
1

Try this example it may help you..

Fiddle

<a href="#" class="transparent-checkbox-thingy" id="1" onclick="check(1)">sdfsf</a>
<input type="checkbox" value="1"  class="newconvo-check" id="2" >


$("a.transparent-checkbox-thingy").click(function(event) {
           // alert('working')
            $('#2').attr("checked","checked");
        });

Comments

1

Try this

  function checkcheck(id)
  {
        $('input[id^="'+id+'"]').prop('checked', true);
  }

working demo : http://jsfiddle.net/8L5k4/1/

Its working fine..But I dont know how its working I tried with $("input#"+id") its not working so I tried this,Its working :)

Comments

0

An id must begin with a letter (not a number). If your id = "1", then its an invalid selector, that's why its not getting checked.

EDIT:

From HTML 4 spec:

ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").

So... your issue may have stemmed from having multiple ids on the page, but you're still going to have broken selectors in some browsers due to invalid markup.

1 Comment

It actually works with a number only, but as Krish said, I had two elements with the same id =)

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.