tags:

views:

45

answers:

2

Hi,

I have a a couple of datetime picker fields and if any of them is empty I want the Submit button to be invisible. I know how to do that but not how to make the button visible if all the datetime picker fields contains text.

I've seen exmaples using keyup but when I use the datetime picker I never put my cursor in the textbox and type something, the datetime picker populates the field.

The button has an name ending with diidIOSaveItem. I can't use ID or name with the datetime pickers.

if ($("input[title='Target Date']").val().length < 1)
{
$("input[name$='diidIOSaveItem']").hide();
}
else
{
$("input[name$='diidIOSaveItem']").show();
}

This hides the button if there's no set target date. How do I show the button if there's something in the field no mater how I put it there?

Thanks in advance.

UPDATE

I know have:

$("input[title='Start Date']").bind('keyup keydown change', function () {
  if ($(this).val() == '') {
    $("input[name$='diidIOSaveItem']").hide();
  } else {
    $("input[name$='diidIOSaveItem']").show();
  }
}).change();

$("input[title='Target Date']").bind('keyup keydown change', function () {
  if ($(this).val() == '') {
    $("input[name$='diidIOSaveItem']").hide();
  } else {
    $("input[name$='diidIOSaveItem']").show();
  }
}).change();

$("select[title='Strategic Objective']").bind('keyup keydown change', function () {
  if ($(this).val() == 0) {
    $("input[name$='diidIOSaveItem']").hide();
  } else {
    $("input[name$='diidIOSaveItem']").show();
  }
}).change();

$("select[title='Strategic Priority']").bind('keyup keydown change', function () {
  if ($(this).val() == 0) {
    $("input[name$='diidIOSaveItem']").hide();
  } else {
    $("input[name$='diidIOSaveItem']").show();
  }
}).change();

If I don't have anything selected in one of the dropdownlists the button is hidden. However, if I select something in the dropdown lists the button becomes visible even though the datetime picker fields are empty. Suggestions?

A: 

If you really want it to be instant (any entry makes it show and it goes away as soon as you clear the input out) I go with this:

$.fn.extend({
  vals: function () {
    var retVals = []; 

    this.each(function () {
      retVals.push($(this).val())
    }); 

    return retVals; 
  }
});

var myInputs = "input[title='Target Date'], input[title='Start Date'], select[title='Strategic Objective']";

$(myInputs).bind('keyup keydown change', function () {
  var entries = $(myInputs).vals();

  for (i in entries) {
    if (entries[i] == '') {
      $("input[name$='diidIOSaveItem']").hide();
      return;
    }
  }

  $("input[name$='diidIOSaveItem']").show();
}).change();

With the updated example you can use vals() to get an array of the values of all of your inputs. In order for it to work with the Select Element you'll need your first Option to look like this: <option value=''> </option>. I think that should be pretty close.

g.d.d.c
Hi g.d.d.c,Thanks for that. However, it doesn't hide the button when the page is first loaded, only if I enter something in the field and then remove it. If I have:if ($("input[title='Target Date']") == ''){$("input[name$='diidIOSaveItem']").hide();}$("input[title='Target Date']").bind('keyup keydown change', function () { if ($(this).val() == '') { $("input[name$='diidIOSaveItem']").hide(); } else { $("input[name$='diidIOSaveItem']").show(); }});it does, but can I do this in a nicer way? Thanks.
peter
@peter - If you've got the entire thing in a `$()` enclosure so you're binding after the page is rendered you can do that by triggering one of the bound events after you bind it. I've updated the example.
g.d.d.c
Thanks, much appreciated
peter
and then if I have several datetime picker fielfs? Like:$("input[title='Start Date']").bind('keyup keydown change', function () { if ($(this).val() == '') { $("input[name$='diidIOSaveItem']").hide(); } else { $("input[name$='diidIOSaveItem']").show(); }}).change();$("input[title='Target Date']").bind('keyup keydown change', function () { if ($(this).val() == '') { $("input[name$='diidIOSaveItem']").hide(); } else { $("input[name$='diidIOSaveItem']").show(); }}).change();I enter data in both, deletes in both then add data in one the button becomes visible
peter
also, if I have a dropdown list I thought I could do somethinbg similiar:$("select[title='Strategic Objective']").bind('change', function () { if ($(this).val(0)) { $("input[name$='diidIOSaveItem']").hide(); } else { $("input[name$='diidIOSaveItem']").show(); }}).change(); but it doesn't work, any ideas?
peter
@peter - I find that extending jQuery to have a vals method helps when you have compound conditions for elements. Also, see my note about making your Select work. `$(this).val(0)` sets the value of the Select to 0 (won't work if there's no option with this value). You need to do `(this).val() == 0` if you want to compare the value to something.
g.d.d.c
A: 

For changes to the field itself, you can always listen to events:

$("input[title='Target Date']").bind('change keyup',function(){
  $("input[name$='diidIOSaveItem']").toggle($(this).val()!='');
});

For values set into the field, trigger change() on the input field. E.g.

$("#setvalue").click(function(){  
  $("input[title='Target Date']").val("hello"); // Set a value
  $("input[title='Target Date']").change(); // Force the change event
});

Code in action.

Gert G