Skip to content Skip to sidebar Skip to footer

Javascript Function To Send Alert Firing Regardless Of Value Returned

The function created to send an alert based on a radio box's value returning as null is firing and sending alert while radio box has a value that should not be null. Somewhere else

Solution 1:

Because each radio button is a control it is own right, you need to check if any of the controls that are linked together (via the name attribute) are checked.

Firstly, getElementsByName() returns an array (notice the s on Elements), so there is no .value for you to check.

(Oh, and be aware that having multiple <label id="yesno"> is invalid, as elements need to have a unique id attribute. In this case you're probably best just removing the id="yesno" completely.)

But it's a lot, lot easier to do this via jQuery...

<scripttype="text/javascript">
  $(function() {
    $(document).submit(function() {
      <%For z = 0 to TotalUnits - 1%>
      if ($("input[name='checkradio<%=z%>']:checked").length == 0)
        alert("Select Yes or No for Needs Repair checkbox <%=z%>");
        returnfalse;
      }
      <%Next%>
      $("submitbutton").click(function() {
        $("#formDVIR").submit();
      });
    });
  });
</script>

By using the selector of input[name='checkradio<%=z%>']:checked you're asking jQuery to find all input controls with a name of checkradio1 (or whatever z is) and only those which are checked. If the length of the resultant jQuery object is more than 1 you know at least one is selected

Solution 2:

push me or so

const myId=document.getElementById(“myId”); myId.addEventListener(“click”, function () { alert(“message and stuff”)});

Post a Comment for "Javascript Function To Send Alert Firing Regardless Of Value Returned"