Hey Kyle,
Thanks for the response! That makes a lot of sense, and is how my current implementation works.
If that's the best route to do this, then I'll share how I'm doing it, and where it gets challenging for me.
CODE DUMP!
var INPUT_SELECTOR = 'input:not(:checkbox), textarea';
/* Listen for activity on any input or textarea's values */
$(document).on('keyup blur focus', INPUT_SELECTOR, function(e) {
var $target = $(e.target);
var $label = $('label[for="' + $target.prop('name') + '"]');
var $error = $target.siblings('.mktoError');
/* We use setTimeout here because we want this to run after other
event handlers */
setTimeout(function() {
/* Check if there's an error and hide the label if there is one */
$label.toggleClass('hide', $error.is(':visible'));
/* Make sure that focusing triggers the label */
$label.toggleClass('focus', $target.is(':focus'));
/* Keep the label triggered if the input has any value */
$label.toggleClass('triggered', !!$target.val());
$target.toggleClass('triggered', (!!$target.val() || $error.is(':visible')));
}, 0);
});
END CODE DUMP!
Here's the issue I'm having: sometimes an error will still be fading in and doesn't meet the "is(':visible')" criteria. This means the label just stays there, oblivious to the fact that it's overlapping with the error message.
I'm using the "is(':visible')" criteria in the first place because Marketo likes to hide/show .mktoError, and they don't necessarily remove the error from the DOM when it clears, so I can't simply check for the .mktoError's existence.
It just seemed inefficient to try and combat this specific problem with opacity on the .mktoError, so I came here looking for a better way. If this is the road we have to go down... any idea on what to do to optimize this pattern?
Thanks so much,
Rowan
P.S - Is there a better way to format code on discussion boards here?