hephestos
hephestos

Reputation: 434

Jquery 1.7 checkbox and checked

While I use this

 jQuery($("select[id='Input"+id+"OdbcParamsId']")).toggle();

and I toggle the select box. I try to check also after the loading of the page if a checkbox is really checked: One option:

 if($( "checkbox[id='Input_" + id+"Isquery_'").attr('checked', 'checked')){....}

Second option :

if $( "checkbox[id='Input_" + id+"Isquery_'").is(':checked')) {....}

However both do not work. Despite the way I typed it is from fellows on google. Then the working option for me is :

$( "#Input_" + id+"Isquery").is(":checked") {...}

And the most bizzare now is that it doesn't fire always the event correctly the following final code of mine:

alert($( "#Input_" + id+"Isquery").is(":checked"));
if (!$( "#Input_" + id+"Isquery").is(":checked")){
    jQuery($("select[id='Input"+id+"OdbcParamsId']")).toggle();
}

And as you see while the event toggle is really working, the selector no. I am a very newbie at JQuery, and I don't understand why. Do we have to use different selectors for different events and methods ? Please somebody explain. Thank you. [EDIT]: ** [HTML CODE] **

<td><div class="input checkbox"><input type="hidden" name="data[Input_41][isquery]" id="Input_41Isquery_" value="0"/><input type="checkbox" name="data[Input_41][isquery]"  class="chks" onclick="jqOnOffInputFormDbToggle(41)" options="True False (choose one)" value="1" id="Input_41Isquery"/><label for="Input_41Isquery"></label></div></td>
<td><select name="data[Input][41][odbc_params_id]" id="Input41OdbcParamsId">
<option value="">(choose)</option>
<option value="52">PGSQL1</option>
</select></div></td>

I have to say that replacing toggle() with show/hide really works the result. But the thing with the selector is a bit of awkward mystery for me

Upvotes: 1

Views: 2422

Answers (4)

khaled_webdev
khaled_webdev

Reputation: 1430

if $("#Input_" + id+"Isquery").is(":checked"){
   $("#Input" + id + "OdbcParamsId").toggle();
}

Upvotes: 0

Terseus
Terseus

Reputation: 2212

First, looks like you have misunderstood how jQuery selectors works, I heavily recommend you to read the jQuery tutorial right now.

Second, your use of toggle is not how it's supposed to be used for; maybe it works but it's not the way to do it, in fact toggle is an event binding function.

Now, I assume you're using at least jQuery 1.6, if not maybe the following code doesn't work.


To get the value of a checkbox (boolean true of false):

$("#input_id").prop("checked");

To modify a checkbox (marked as checked, change the value to false to uncheck it):

$("#input_id").prop("checked", true);

To get the value of a select (that is, the value of the selected option):

$("#select_id").find("option:selected").value();

To modify the value of a select:

$("#select_id").val("option_value");

Here you have a JsFiddle with some examples.

Upvotes: 0

charlietfl
charlietfl

Reputation: 171669

Biggest problem is selector starting with "checkbox" is invalid, there is no tagName "checkbox". There is a jQuery selector ":checkbox", however your first "if" using attr('checked','checked') is not properly set up either.

The last solution is the most efficient anyway since using an ID selector is the most efficient to process, as opposed to $('tagName[attribute="value"]') and it is easier to read

EDIT: more code was added after this was written

Along the lines of my "ID selector is best" , change :

   jQuery($("select[id='Input"+id+"OdbcParamsId']")).toggle();

To:

 jQuery('#Input'+id+'OdbcParamsId').toggle();

Use "jQuery" or "$" but mixing them up is causing you confusion, they are the same thing

Upvotes: 2

Th0rndike
Th0rndike

Reputation: 3436

The toggle says:

select[id=Input+id+"OdbcParamsId']"]

and the selector says:

select["#Input_"+id+"Isquery"]

so, is it Input or Input_ ?

Upvotes: 0

Related Questions