views:

34

answers:

3

Ok, I'm really starting to have a bad day and the ol' brain isn't working. I can't think of what I should be searching for, or even find previous examples of this kind of thing.

I have a HTML form with three fields;

  • select: disposition_status
  • text: disposition_datetime_hasrecord
  • text: disposition_description

The select field disposition_status has the following values:

  • 1 - in
  • 2 - out

When a user selects the item in disposition_status with the value of 1 (in), I want to clear any text in the fields disposition_datetime_hasrecord and disposition_description.

If anyone has an example or can point me in the right direction for achieving this it would be appreciated.

+2  A: 

Try this:

$('select[name=disposition_status]').change(function() {
    if ($(this).val() == 1) {
        $('*[name=disposition_datetime_hasrecord]').val('');
        $('*[name=disposition_description]').val('');
    }
});
Graphain
Are you a programmer? "this doesn't work" is not a valid response. Let me know *how* it doesn't work and I might be able to help. Do you have `name` s as listed in the script on each element?
Graphain
+2  A: 

It's ok, Sunday nights are a tough night to code anyways ;-)

To do this, you would use the jQuery change method to catch any changes within the menu. From there, you could setup a simple case statement to decide what to do, e.g.:

 $("#disposition_status").change(function() {
     if ($(this).val() == 1) {
         $("#disposition_datetime_hasrecord").val("");
         $("#disposition_description").val("");
     }
 });

This is assuming you have those set as ID's, if not you can use the selector "*[name=disposition_status]" etc. to select them.

Edit: Oh drats! Just beaten by someone else ;-) Same thought process at least :)

Bartek
Only just, remarkably similar code though.
Graphain
+3  A: 

demo

$('#disposition_status').change(function() {
    if (this.value == 1) {
        $('#disposition_datetime_hasrecord').val('');
        $('#disposition_description').val('');
    }
});​

edit: based on comment

Okay, I see now the problem, it should be wrap inside

$(document).ready(function() { 
    //code here 
}); 

sorry jsfiddle do it internally (I did set it actually).. so just put the codes inside like

$(document).ready(function() { 
   $('#disposition_status').change(function() {
      if (this.value == 1) {
        $('#disposition_datetime_hasrecord').val('');
        $('#disposition_description').val('');
      }
   });​
}); 

explanation here

Reigel
@Reigel - Just added "disposition_", since it was part of his field names. I like your use of "this.value" instead of "$(this).val()". Worked fine.
Gert G
Oh right, this.value is what he should use as he's within the scope of `disposition_status`. I always mess that up and use `$(this).val()` -- Nice catch!
Bartek
got that right Bartek... ;)
Reigel
thewinchester
@thewinchester - ahh the problem is you must put `value` `attribute` in `option`... like this, http://jsfiddle.net/dPVyq/2/
Reigel
@Reigel: Yeah, that's what I thought too. The bizarre thing is that your latest example on jsfiddle works properly in IE8 without a hitch. Put this code into my site, and it doesn't go a thing. Checked to make sure they match up exactly, and no dice. Even saved out the resulting iFrame code from jsfiddle to make sure it wasn't something I did and could do a comparison. Strangely, problem still exists despite being exactly the same and even using latest jQuery library. Cache cleared, IE8 running in standards mode... perplexed.
thewinchester
hmmm can you provide a link of your site for me to look at? or please try my new update... http://jsfiddle.net/dPVyq/3/ when you change value on select, it should alert 0... see if it does alert 0; and copy to your site too... and see if it alerts too..
Reigel
Yeah, it is a bit odd. Tried your latest jsfiddle version, and it works perfectly with the alert dialogue displaying as expected. Dump that code into a plain from scratch HTML page on chez intranet as I've been doing, load it up, and no dice. Page is on an intranet, so can't be accessed from outside the firewall. Have dumped a copy of the source code to http://bit.ly/cy3N2K to have a look at. Thanks for the continued help.
thewinchester
please see my edit... and make sure you did not forget to put `value` `attribute` on `option` s
Reigel
@Reigel: Sorry, this was all my mistake - and thanks for the help. Have realised where I went wrong and there was no way you would have seen the issue in the examples I provided. Long story, but I'm facepalming myself now.
thewinchester
so it works now?
Reigel