Hello, i have this form for adding videos. Now, there's a input field for "title" and i want that to disappear if you have pressed on the checkbox "tryout". How should i do that in JS.. i use jquery.
+1
A:
you can just write inside document.ready function.
$("#chkboxId").clicked(function(){
if($(this).attr("checked"))
$("#title").hide();
else
$("#title").show();
});
Amitabh
2010-02-14 11:33:30
Since I believe the OP (and anyone really) will in practice want/need to have more flexible control over display, I would suggest using toggleClass('removed', $this.checked) over element style manipulation, where 'removed' is a CSS class with display:none.
annakata
2010-02-14 11:38:03
this does not work?
Karem
2010-02-14 12:43:44
What is the error u r getting?
Amitabh
2010-02-14 15:56:34
A:
<input type="checkbox" id="tryout" name="tryout" onclick="javascript:$('#tryout').hide();">Tryout</input>
Alexander Shirshov
2010-02-14 11:33:43
+1
A:
Assuming you have correct ids assigned for both fields something like this will work:
$('#tryout').click(function () {
$('#title').toggle(!$(this).attr('checked'));
});
RaYell
2010-02-14 11:33:45
IMO you have to pass !($this).attr('checked') because we want to hide the element when checkbox is checked, and while using toggle, false value hides element. Second thing, we don't have to wrap this with jQuery object to get checked value. Just use this.checked.
Rafael
2010-02-14 11:36:27
Indeed the condition for toggle should be negated. Second thing: you don't have to do it but I prefer keeping it that way. I don't like mixing jQuery code with "pure" JS DOM operations.
RaYell
2010-02-14 12:28:27
Yes, with: <input type="text" id="title" name="title"><br><input type="checkbox" id="tryout" name="tryout">Tryout</input><br> when i put a check in the checkbox, nothing happends. Nothing when i remove the check too..
Karem
2010-02-14 19:56:09
If you are using firefox then check in Firebug that you don't have any errors in your JS code. You could try adding an alert inside the click function and see if that code is triggered. If it's not then perhaps you didn't put it in the correct spot.
RaYell
2010-02-14 21:37:18