0

This may be a strange question -- Right now I have a variable that is full of HTML, and I want to use jQuery (or JS) to search that varaible for inputs with checkboxes, and return the information.

So:

alert($(this).parent().parent().html())
var thisIsThat = $(this).parent().parent().html();
alert(thisIsThat)
var Awesome = $(thisIsThat).find('input:checked');

And then after I get that variable, after a successful ajax call, I want to change a specific attribute inside of it, like so:

$(Awesome).attr('value', 'false');

Right now, "Awesome" is returning nothing, which then doesn't allow me to change the attribute like I want to. I may be on the wrong direction as well -- any advice appreciated!

streetlight
  • 5,968
  • 13
  • 62
  • 101

3 Answers3

3

Use this

var thisIsThat = $(this).parent().parent();
alert(thisIsThat)
var Awesome = $(thisIsThat).find('input:checked');

In this case thisIsThat is a object and you can find anything using that object

StaticVariable
  • 5,253
  • 4
  • 23
  • 45
  • Thank you for your help! This worked perfectly. I just removed the ':checked' selector as I wanted to hit both checked and unchecked inputs (and there's only one input in this row). – streetlight Sep 28 '12 at 13:02
0

This is an example showing the same basic idea running off of a string which finds the checkbox fine and unchecks it.

<div id="out"></div>​
<script>
    var htmlStr = '<div><input type="checkbox" checked="checked"/></div>';
    var temp = $(htmlStr);
    var cb = temp.find("input:checked");
    cb.attr("checked",false);
    jQuery("#out").append(cb);
</script>

jsfiddle

The problem I am betting is that you are checking the checkbox manually. It will not update the DOM attribute when you do that.

Here is a basic example to show you the problem.

<div id="one">
    <input type="checkbox" />
</div>
<button>Tell</button>​

<script>
    function tellMe(){
        var myDiv = jQuery("#one");
        var html1 = myDiv.html();
        console.log(html1);
    }

    jQuery("button").click(tellMe);  
</script>

Take a look this fiddle of the code above.

Open up the console, and click on the button. You will see it unchecked. Check the checbox and click the button again, same html is outputted even though the checkbox is checked.

epascarello
  • 204,599
  • 20
  • 195
  • 236
  • Thank you for your help on this! I was running the code inside of a loop, so it was updating regularly I believe, so the other solution worked. Thank you again! – streetlight Sep 28 '12 at 13:20
-3

change

var Awesome = $(thisIsThat).find('input:checked');

to

var Awesome = $(thisIsThat).find('input[type=checked]');

now loop over it

Awesome.each(function(){
  if($(this).is(':checked'))
    {
     $(this).attr('checked',false);
    }
});
Neeraj
  • 158
  • 5