views:

233

answers:

3

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
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
this does not work?
Karem
What is the error u r getting?
Amitabh
A: 
<input type="checkbox" id="tryout" name="tryout" onclick="javascript:$('#tryout').hide();">Tryout</input>
Alexander Shirshov
IT only hides, but not appear if you mark off the SC tryout?
Karem
+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
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
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
I cant get this to work? It doesnt hide when i press the checkbox
Karem
Are you sure both selectors match proper elements in your page?
RaYell
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
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
ive put it under document load and it works great now. Thank you.
Karem