Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Senior Coder crmpicco's Avatar
    Join Date
    Jan 2005
    Location
    Mauchline, Scotland
    Posts
    1,094
    Thanks
    15
    Thanked 1 Time in 1 Post

    Lightbulb make javascript code more dynamic

    Code:
    else if (segments == 6)
                            {
                                if ((document.forms["form"].elements["ad1"].value == "") && (document.forms["form"].elements["ad2"].value != "") && (document.forms["form"].elements["ad3"].value != "") && (document.forms["form"].elements["ad4"].value != "") && (document.forms["form"].elements["ad5"].value != "") && (document.forms["form"].elements["ad6"].value == ""))
                                {
                                    el.innerHTML = el.innerHTML + seatArr[s-2];
                                }
                                if ((document.forms["form"].elements["ad1"].value == "") && (document.forms["form"].elements["ad2"].value == "") && (document.forms["form"].elements["ad3"].value == "") && (document.forms["form"].elements["ad4"].value == "") && (document.forms["form"].elements["ad5"].value != "") && (document.forms["form"].elements["ad6"].value == ""))
                                {
                                    el.innerHTML = el.innerHTML + seatArr[s-5];
                                }
                                if ((document.forms["form"].elements["ad1"].value == "") && (document.forms["form"].elements["ad2"].value == "") && (document.forms["form"].elements["ad3"].value == "") && (document.forms["form"].elements["ad4"].value != "") && (document.forms["form"].elements["ad5"].value == "") && (document.forms["form"].elements["ad6"].value == ""))
                                {
                                    el.innerHTML = el.innerHTML + seatArr[s-4];
                                }
                                if ((document.forms["form"].elements["ad1"].value == "") && (document.forms["form"].elements["ad2"].value != "") && (document.forms["form"].elements["ad3"].value == "") && (document.forms["form"].elements["ad4"].value == "") && (document.forms["form"].elements["ad5"].value == "") && (document.forms["form"].elements["ad6"].value == ""))
                                {
                                    el.innerHTML = el.innerHTML + seatArr[s-2];
                                }
                                if ((document.forms["form"].elements["ad1"].value == "") && (document.forms["form"].elements["ad2"].value == "") && (document.forms["form"].elements["ad3"].value != "") && (document.forms["form"].elements["ad4"].value == "") && (document.forms["form"].elements["ad5"].value == "") && (document.forms["form"].elements["ad6"].value == ""))
                                {
                                    el.innerHTML = el.innerHTML + seatArr[s-3];
                                }
                                if ((document.forms["form"].elements["ad1"].value == "") && (document.forms["form"].elements["ad2"].value == "") && (document.forms["form"].elements["ad3"].value == "") && (document.forms["form"].elements["ad4"].value == "") && (document.forms["form"].elements["ad5"].value == "") && (document.forms["form"].elements["ad6"].value != ""))
                                {
                                    el.innerHTML = el.innerHTML + seatArr[s-6];
                                }
                                if ((document.forms["form"].elements["ad1"].value == "") && (document.forms["form"].elements["ad2"].value != "") && (document.forms["form"].elements["ad3"].value == "") && (document.forms["form"].elements["ad4"].value != "") && (document.forms["form"].elements["ad5"].value == "") && (document.forms["form"].elements["ad6"].value != ""))
                                {
                                    el.innerHTML = el.innerHTML + seatArr[s-2];
                                }
    I have written this JavaScript code which is just one condition in the function. Can anyone see a way of making it more dynamic and cutting down the code?

    I cant see it,

    :-(

    Picco

  • #2
    Senior Coder
    Join Date
    Feb 2003
    Posts
    1,665
    Thanks
    0
    Thanked 27 Times in 25 Posts
    Firstly, you should look at committing those objects to simpler variable names to save on the lengthy repetitions.

    e.g.
    Code:
    var ad1Obj = document.forms["form"].elements["ad1"];
    Subsequent instances of document.forms["form"].elements["ad2"] can be switched for ad2Obj (and so on for the other adx objects.

    Instances of this code structure…
    Code:
    el.innerHTML = el.innerHTML + … ;
    …can be replaced with…
    Code:
    el.innerHTML += … ;

    This should make the code a little lighter, but I'm a little too rusty with js to know if more can be done to make the body of if statements any more efficient than that.

    I'd look into switch statements, though I'm not sure they can be applied to such a complex array of possibilities. :rusty:


  •  

    Posting Permissions

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