Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5945091
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:37:59+00:00 2026-05-22T16:37:59+00:00

I have this two HTML Form buttons with an onclick action associated to each

  • 0

I have this two HTML Form buttons with an onclick action associated to each one.

<input type=button name=sel value="Select all" onclick="alert('Error!');">
<input type=button  name=desel value="Deselect all" onclick="alert('Error!');">

Unfortunately this action changes from time to time. It can be

onclick="";>

or

onclick="alert('Error!');"

or

onclick="checkAll('stato_nave');"  

I’m trying to write some javascript code that verifies what is the function invoked and change it if needed:

var button=document.getElementsByName('sel')[0];
// I don't want to change it when it is empty or calls the 'checkAll' function
if( button.getAttribute("onclick") != "checkAll('stato_nave');" &&
    button.getAttribute("onclick") != ""){
    //modify button
    document.getElementsByName('sel')[0].setAttribute("onclick","set(1)");
    document.getElementsByName('desel')[0].setAttribute("onclick","set(0)");
} //set(1) and set(0) being two irrelevant function

Unfortunately none of this work.
Going back some steps I noticed that

alert( document.getElementsByName('sel')[0].onclick);

does not output the onclick content, as I expected, but outputs:

function onclick(event) {
    alert("Error!");
}

So i guess that the comparisons fails for this reason, I cannot compare a function with a string.
Does anyone has a guess on how to distinguish which function is associated to the onclick attribute?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T16:38:00+00:00Added an answer on May 22, 2026 at 4:38 pm

    This works

    http://jsfiddle.net/mplungjan/HzvEh/

    var button=document.getElementsByName('desel')[0];
    // I don't want to change it when it is empty or calls the 'checkAll' function
    var click = button.getAttribute("onclick");
    if (click.indexOf('error') ) {
    
        document.getElementsByName('sel')[0].onclick=function() {setIt(1)};
        document.getElementsByName('desel')[0].onclick=function() {setIt(0)};
    
    }
    
    function setIt(num) { alert(num)}
    

    But why not move the onclick to a script

    window.onload=function() {
      var button1 = document.getElementsByName('sel')[0];
      var button2 = document.getElementsByName('desel')[0];
      if (somereason && someotherreason) {
        button1.onclick=function() {
          sel(1);
        }
        button2.onclick=function() {
          sel(0);
        }
      }
      else if (somereason) {
        button1.onclick=function() {
          alert("Error");
        }
      }
      else if (someotherreason) {
        button1.onclick=function() {
          checkAll('stato_nave')
        }
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two buttons inside a form call: <% using (Html.BeginForm()) {%> <input type=submit
i have a textarea and two buttons like <form name=form1> <textarea name=text1> HTML Codes
For example, I have a HTML form with several input buttons, <form action=/index.php method=post>
I have two HTML buttons; one submits a form and one closes the container
I have two submit buttons and one form. How do I check what submit
I want to use a form that has two submit buttons. Each button calls
Say, I have two HTML elements - an input field and a button. The
Actually I have this piece of HTML code <form method=POST action=dosomething.php> <fieldset> <legend>My account</legend>
I have two submit buttons in one form and both the submit are linked
Is there any way to have this: [one[two]][three] And extract this with a regex?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.