This project has moved. For the latest updates, please go here.

Hide Field based on SPFieldNumber

Feb 25, 2015 at 6:29 AM
Hey!

I have another Problem here. I want to hide the field "einsfeld" if the Number in the Field "eins" is "0". Otherwise the field must be showed.

My code:
$(document).ready(function () {

var eins = SPUtility.GetSPField('1');
var einsfeld = SPUtility.GetSPFieldByInternalName('_x0031_');

einsfeld.Hide();

if (eins.GetValue() === '0') {
einsfeld.Hide();
}
else { 
einsfeld.Show();
}

});
It doesn't work. I guess I have to integrate a Change eventhandler? But how?

Thank you in advance!
Coordinator
Feb 25, 2015 at 11:08 PM
For Number fields, GetValue will return a JavaScript number so you want to change your comparison:
if (eins.GetValue() === 0) {
The way you have it right now, the code will run once when the document is ready to show/hide einsfeld. If you want to show/hide einsfeld when the user changes it then you'll need to add some more code.
$(eins.Textbox).change(function(evt){
    if (eins.GetValue() === 0) {
        einsfeld.Hide();
    } else { 
        einsfeld.Show();
    }
});
Feb 26, 2015 at 6:17 AM
Thank you Kit!

Guess I'm still doing something wrong though:
$(document).ready(function () {
var eins = SPUtility.GetSPField('1');
var einsfeld = SPUtility.GetSPFieldByInternalName('_x0031_');

if (eins.GetValue() === 0) {
einsfeld.Hide();
}
else {
einsfeld.Show();
}
});

var eins = SPUtility.GetSPField('1');

$(eins.Textbox).change(function(evt){
    if (eins.GetValue() === 0) {
        einsfeld.Hide();
    } else { 
        einsfeld.Show();
    }
});
</script>
It still doesn't work...
Coordinator
Feb 26, 2015 at 2:50 PM
How about something like this:
<script>
$(document).ready(function () {
   var eins = SPUtility.GetSPField('1');
   var einsfeld = SPUtility.GetSPFieldByInternalName('_x0031_');
   
   var handleEinsFieldChange = function(evt){
       if (eins.GetValue() === 0) {
           einsfeld.Hide();
       } else { 
           einsfeld.Show();
       }
   };
   
   // run whenever the eins textbox changes
   $(eins.Textbox).change(handleEinsFieldChange);
   
   // run once when document is ready
   handleEinsFieldChange();
});
</script>
Feb 27, 2015 at 6:15 AM
Perfect! It works!
Thank you!!