www.webdeveloper.com
Results 1 to 8 of 8

Thread: Better way to evaluate than eval()?

  1. #1
    Join Date
    Dec 2002
    Location
    St. Louis, MO, USA
    Posts
    1,582

    Question Better way to evaluate than eval()?

    Hello, everyone.

    I have a script that will loop through all the INPUT elements of a form; if the input is a submit it will disable the submit (or enable the submit, depending upon what is passed to the function.)

    I hate using eval() - I avoid it whenever I can! Is there a better way to dynamically process the ".disabled = 'true'/'false';" portion?

    Code:
    // a function that will (dis/en)able all submit buttons
    function edSubmit(thisStatus) { // thisStatus is either "true" or "false"
    	var elementArray = new Array();
    	elementArray = document.bft.getElementsByTagName("input");
    	thisVar = ""; arrayLength = elementArray.length;
    	for(i=0;i<arrayLength;i++) {
    		if(/submit/i.test(elementArray[i].type)) {
    			eval("document.bft."+elementArray[i].name+".disabled = " + thisStatus + ";");
    			}
    		}
    	}
    Thanks,

    ^_^

  2. #2
    Join Date
    Aug 2007
    Posts
    3,767
    document.bft[elementArray[i]].name.disabled

    In fact, you can use that syntax all the time, for example, if you really wanted to:
    Code:
    window["document"]["bft"][elementArray[i]]["name"]["disabled"]

  3. #3
    Join Date
    Dec 2002
    Location
    St. Louis, MO, USA
    Posts
    1,582
    Thanks for the reply, Declan.

    The way you have it typed, it didn't work:
    document.bft[elementArray[i]].name.disabled

    But I tried it like:
    document.bft[elementArray[i].name].disabled

    .. and it will disable the submit buttons; but it will not re-enable the submit buttons when the "false" parameter is passed.

    Likewise for:
    window["document"]["bft"][elementArray[i]]["name"]["disabled"]

    I changed it to:
    window["document"]["bft"][elementArray[i]["name"]]["disabled"]

    .. and it will disable but not re-enable the submit buttons.
    Code:
    1. document.bft[""+elementArray[i].name+""].disabled = thisStatus; 
    2. window["document"]["bft"][elementArray[i]["name"]]["disabled"] = thisStatus;
    3. document.bft[""+elementArray[i].name+""].disabled = ""+thisStatus; 
    4. window["document"]["bft"][elementArray[i]["name"]]["disabled"] = ""+thisStatus;
    Works for disabling the submits, but won't re-enable them, and is not throwing an error for console. Any ideas?

    ^_^

  4. #4
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    Code:
    document.bft[elementArray[i].name].removeAttribute('disabled');

  5. #5
    Join Date
    Dec 2002
    Location
    St. Louis, MO, USA
    Posts
    1,582
    Code:
    Error: uncaught exception: [Exception... "Cannot modify properties of a WrappedNative"  nsresult: "0x80570034 (NS_ERROR_XPC_CANT_MODIFY_PROP_ON_WN)"  location: "JS frame :: chrome://global/content/bindings/autocomplete.xml :: onxblpopuphiding :: line 825"  data: no]

  6. #6
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Quote Originally Posted by Fang View Post
    Code:
    document.bft[elementArray[i].name].removeAttribute('disabled');
    I guess better:
    Code:
    document.forms['bft'].elements[elementArray[i].name][i]
    to avoid any mismatches.

  7. #7
    Join Date
    Dec 2002
    Location
    St. Louis, MO, USA
    Posts
    1,582
    Quote Originally Posted by Kor View Post
    I guess better:
    Code:
    document.forms['bft'].elements[elementArray[i].name][i]
    to avoid any mismatches.
    Same error message.

    ^_^

  8. #8
    Join Date
    Dec 2002
    Location
    St. Louis, MO, USA
    Posts
    1,582
    I think I'm going to be stuck with using eval() for this. I've tried many different bracketed syntax formulas, and they all will disable the submit, but (for whatever reason) will not enable the submit upon either successful response or timeout due to lack of response.

    As much as I hate eval(), it seems to be the only option (at least, for now.)

    Thanks to everyone who offered suggestions/ideas. Much appreciated.

    ^_^

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles