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 a text box and a button, which is described with the HTML/CSS
If my HTML form contains two form inputs ( input1 and input2 ), I
I've got a standard HTML form, which is split into two sections using fieldset
I have a formset that have two forms. forms: class PresClinicForm(forms.Form): _names = list(PresClinic.objects.values_list('pres_clinic_id',
I have a table in which i append rows on onclick event, the row
I have two views: create and edit. Both share a strongly typed editor template
I have a site running on windows server 2008. The site is HTML, and
I have a form field in Drupal which I need some help with. The
I have a little problem, and an need your profesisonal... So i have two
In my Web Application, i have a data table(Displayed from DB using PreparedStatement). Each

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.