I’m trying to get a button (input field or button field) value each time a form is submitted. I think I’m having some logic issues though because the second one does not work.
Here it is on jsFiddle
http://jsfiddle.net/uqj88/23/
This is specifically the piece that’s not working properly. If it doesn’t find input where type is submit, it doesn’t even seem to look for a button element.
$('form').submit(function () {
if ($(this).children("input[type='submit']") != "") {
submitButton = $(this).children("input[type='submit']");
if (submitButton.attr("value") != ""){
linkText = submitButton.attr("value") + " - Button";
}
else if ((submitButton.attr("value") === "") && (submitButton.prop("id") != "")) {
linkText = submitButton.prop("id") + " - Button";
}
else if ((submitButton.attr("value") === "") && (submitButton.prop("class") != "")) {
linkText = submitButton.prop("class") + " - Button";
}
else {
linkText = "Form Submitted - Button";
}
}
else if ($(this).children("button") != "") {
submitButton = $(this).children("button");
linkText = submitButton.text() + " - Button";
}
window.alert(linkText);
});
This line:
should be:
jQuery objects are always objects, regardless of whether or not they contain elements. An object will never be equal to
"".Repeat the same change for your else if statment.