Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop link action if onclick function returns false

Here is a function that simply checks if a radio button has been selected on a form element.

function validate_selected(element) {
    var radios = document.getElementsByName(element);
    var formValid = false;

    var i = 0;
    while (!formValid && i < radios.length) {
      if (radios[i].checked) formValid = true;
      i++;        
    }

    if (!formValid) alert("Must select one before moving on!");
    return formValid;
}

Here is my link that I want disabled if the function returns false. Right now the link shows the alert but after the alert the link sends the user forward.

<a href="#m2" data-role="button" onclick="validate_selected('owner');"><p style="font-size:<?php echo $size1.'px'; ?>;">Next</p></a>

I want the link to be disabled if the function returns false.

like image 605
user2843198 Avatar asked Oct 03 '13 14:10

user2843198


People also ask

How can I disable href If onclick is executed?

To disable href if onclick is executed with JavaScript, we call preventDefault to stop the default navigation action. document. getElementsById("ignore-click"). addEventListener("click", (event) => { event.

What does onclick return false do?

using return false in an onclick event stops the browser from processing the rest of the execution stack, which includes following the link in the href attribute. In other words, adding return false stops the href from working. In your example, this is exactly what you want.

Does Onclick work with links?

This is a type of JavaScript link - the onclick attribute defines a JavaScript action when the 'onclick' event for the link is triggered (i.e. when a user clicks the link) - and there is a URL present itself in the onclick attribute.

What happens when the onclick event handler for a radio button returns false?

Selecting a radio button will trigger an onclick event. If your event handler returns false, Internet Explorer will not change the state of any radio buttons.


2 Answers

You almost got it, you needed to add the 'return' in the function aswell:

onclick="return validate_selected('owner');"

Also a small tip, add a break:

while (!formValid && i < radios.length) {
    if (radios[i].checked){
        formValid = true; 
        break; // No need to continue any more, we've found a selected option
    }
    i++;        
}

I've changed your onclick from inline to the proper way to handle events, and made a working demo. Just remove the onclick from the button and add this:

document.getElementById('button').onclick =function(){ validate_selected('owner'); };

http://jsfiddle.net/WX6v7/1/ (added some radiobuttons in the example)

like image 101
Martijn Avatar answered Nov 14 '22 23:11

Martijn


You need to actually return the value in the onclick that your function returned:

onclick="return validate_selected('owner');">
like image 33
mattytommo Avatar answered Nov 14 '22 22:11

mattytommo