I am using the latest and greatest jQuery. I am trying to make my code efficient and reusable throughout a specific site and want consolidate a block.
I have a piece of code that listens for a save button being clicked. When the save button is clicked, it figures out which selector was click and gets its id and value.
This code works perfectly for text boxes. I just want to be able to run select boxes, radio buttons, and check boxes through it too. I need to handle the data differently depending on whether it’s a checkbox, radio, or text field.
If I know the id of a selector, how can I easily test which type of selector it is?
In pseudocode:
if (SelectorType == 'radio') {
// do radio stuff
} else if (SelectorType == 'text') {
// do test field stuff
} else if (SelectorType == 'checkbox') {
// do checkbox stuff
}
ANSWER
This is what I was trying to get at:
var ThisField = $(this).parent().parent().children("td:eq(1)").children(":input").attr("id");
var FieldType = $("#"+ThisField).prop("type");
if (FieldType == "select-one") {
alert("You are trying to save select box info!");
} else if (FieldType == "text") {
alert("You are trying to save text info!");
} else if (FieldType == "checkbox") {
alert("You are trying to save checkbox info!");
} else if (FieldType == "radio") {
alert("You are trying to save radio info!");
}
jQuery’s prop method can give you the input type:
or even simpler: