Jump to content

[SOLVED] Maximum length for onclick?


53329

Recommended Posts

I have an onclick function that works when I only have it pulling values from a few elements but when the number starts to increase I get errors that say none of my html inputs have any properties.  The onclick I'm using is about 2500 characters long.  Is there a limit to the length of the code?

 

Paraphrased the code is

onclick="myfunction((document.getElementById('home').checked==true ? 1 : 0) + (document.getElementById('help').checked==true ? 1 : 0))"

 

Just repeat the inside about 20-30 times instead of twice to get an idea of the length.

 

So I'm not sure if the problem is the length of the code in the onclick as for all I know it is a problem with a maximum length for a function argument.  I might be able to get around this a few ways but first I need to know why I'm getting the error.

 

Firefox error console says:

document.getElementById("home") has no properties

 

But again its only when I happen to have a lot of getElementById's.  If its just the one or a few it seems to work fine.

Link to comment
Share on other sites

I'm not sure of a max length, but why would you want to put that much "functionality" in the trigger? In my opinioin, a trigger is just that - just a trigger to call a function. I would suggest just using the element IDs in the trigger and have the first part of the function do the determination as to whether the elements are checked or not.

 

Here is some sample code (not tested, but logic should be sound)

onclick="myfunction('home'+'/'+'help')"

Add as many IDs as needed separated by a delimeter character (in this case the forward slash)

 

function myfunction(params) {
    var paramAray = params.split('/');
    var inputVal

    for (var i=0; i<paramAray.length; i++) {
        inputVal += (document.getElementById(paramAray[i]).checked==true)?1:0;
    }

    //Continue with function as before and use inputVal

}

Link to comment
Share on other sites

That was one of my ideas actually.  Problem is that its taking information and submitting it to the server with ajax.  I guess I could look for a more viable way to submit forms with ajax.

 

And I think I already got one.  So obvious I don't know how I missed it.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.