1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

if, else

Discussion in 'JavaScript' started by clay5366, Jun 14, 2018.

  1. #1
    Hello,

    I am attempting to create a script to disable fields when the 'Status' field is = to 'Closed'

    I added the ifclosed to the onload portion of the page.
    SEMrush
    but can't seem to get it to work. There is only one form on the page. What am I missing?

    Thanks
    David

    <script type="text/javascript">
    function ifclosed()
    {
      with (document.forms[1])
        {
      
            if (STATUS.value = "CLOSED")
                  {document.forms[1].VEREFF.disabled=true};
                {document.forms[1].CMNTS.disabled=true};
                {document.forms[1].fp_submit.disabled=true};
            else
                {document.forms[1].VEREFF.disabled=false};
                {document.forms[1].CMNTS.disabled=false};
                {document.forms[1].fp_submit.disabled=false};
                
        }
    }
    </script>
    PHP:
     
    Last edited by a moderator: Jun 14, 2018
    clay5366, Jun 14, 2018 IP
    SEMrush
  2. hdewantara

    hdewantara Active Member

    Messages:
    416
    Likes Received:
    35
    Best Answers:
    17
    Trophy Points:
    80
    #2
    Hi,
    I'd usually use following pattern below. Then just put whole script to the bottom of page before closing body tag </body>...
    <script>
    var myForm = document.forms[1];
    if (myForm.STATUS.value = "CLOSED"){
      myForm.VEREFF.setAttribute('disabled', 'disabled');
      myForm.CMNTS.setAttribute('disabled', 'disabled');
      myForm.fp_submit.setAttribute('disabled', 'disabled');
    }
    else{
      myForm.VEREFF.removeAttribute("disabled");
      myForm.CMNTS.removeAttribute("disabled");
      myForm.fp_submit.removeAttribute("disabled");
    }
    </script>
    Code (JavaScript):
    See any errors on your Web Console?

    Hendra
     
    hdewantara, Jun 14, 2018 IP
    sarahk likes this.
  3. clay5366

    clay5366 Member

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #3
    Hello Hendra,

    thanks for the quick reply.

    I gave that a whirl and something odd is occurring. The fields are disabling, however it seems that the status is temporarily changing to CLOSED. If I remove the script, the status is back to open. I have a few test records to mess with and it happen to all of them.

    David
     
    clay5366, Jun 15, 2018 IP
  4. hdewantara

    hdewantara Active Member

    Messages:
    416
    Likes Received:
    35
    Best Answers:
    17
    Trophy Points:
    80
    #4
    Hi David,
    Err, I thought you are trying to change the fields (based on status) but not the status itself; what are you actually want to achieve? And perhaps an URL link to a (test) page here may help me understand this?
     
    hdewantara, Jun 15, 2018 IP
  5. clay5366

    clay5366 Member

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #5
    I am looking to change the fields to disabled in the event that the status field has a value of 'CLOSED'. Other wise the fields are not disabled. This way a user cannot change data after the record has been 'CLOSED'.

    Sorry...no link. The site is internal only.

    David
     
    clay5366, Jun 15, 2018 IP
  6. clay5366

    clay5366 Member

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #7
    Ah...I think I have it. I did change it a bit.

    <script>
    var myForm = document.forms[1];
      
            if (myForm.STATUS.value != "OPEN")
        {
                  myForm.DOCNO.setAttribute("readonly", true);
                  myForm.PLANNO.setAttribute("readonly", true);
                  myForm.STATUS.setAttribute("readonly", true);
                  myForm.DESCR.setAttribute("readonly", true);
                  myForm.VEREFF.setAttribute("readonly", true);
                  myForm.CMNTS.setAttribute("readonly", true);
                myForm.fp_submit.setAttribute("disabled", "disabled");
        }
            else
        {
                  myForm.DOCNO.removeAttribute("readonly");
                  myForm.PLANNO.removeAttribute("readonly");
                  myForm.STATUS.removeAttribute("readonly");
                  myForm.DESCR.removeAttribute("readonly");
                  myForm.VEREFF.removeAttribute("readonly");
                  myForm.CMNTS.removeAttribute("readonly");
                myForm.fp_submit.removeAttribute("disabled");
        }
                
    </script>
    PHP:
     
    Last edited by a moderator: Jun 15, 2018
    clay5366, Jun 15, 2018 IP
  7. deathshadow

    deathshadow Acclaimed Member

    Messages:
    8,581
    Likes Received:
    1,535
    Best Answers:
    223
    Trophy Points:
    515
    #7
    Silly question on my part, but what does the FORM look like? Are these all in a consistent fieldset? If so why hardcode each and every one of these inputs instead of simply walking the DOM?

    also, are you sure you mean = "closed" and not == ?!?

    NOT that I'd be relying on array indexed .Forms since what if you re-arrange the content and/or add another form? That's why accessing by ID is your friend...

    Though the way this is coded it LOOKS like something that probably shouldn't even be handled client-side in the first place.
     
    deathshadow, Jun 15, 2018 IP
    hdewantara likes this.
  8. hdewantara

    hdewantara Active Member

    Messages:
    416
    Likes Received:
    35
    Best Answers:
    17
    Trophy Points:
    80
    #8
    ahh... good catch ;)
     
    hdewantara, Jun 15, 2018 IP
  9. clay5366

    clay5366 Member

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #9
    My coding is somewhat limited. I can typically get it done. At times need some assist. Nothing I create is accessed from outside the company.
     
    clay5366, Jun 15, 2018 IP
  10. clay5366

    clay5366 Member

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #10
    I am however running into something odd. The code works in Chrome and Firefox, but not IE. However this does...

    <script>
    
        {
                  document.forms[0].Shop_Order.setAttribute("readOnly", "readOnly");
                  document.forms[0].Shop_Order.style="background-color: #E0E0E0";
        }
                
    </script>
    PHP:
    I have been trying different renditions of the readOnly attribute.
     
    Last edited by a moderator: Jun 15, 2018
    clay5366, Jun 15, 2018 IP
  11. deathshadow

    deathshadow Acclaimed Member

    Messages:
    8,581
    Likes Received:
    1,535
    Best Answers:
    223
    Trophy Points:
    515
    #11
    IE can be a stone cold bitch about changing the attributes on form elements. Particularly ones that control its behavior like readonly, disabled and type. Type in particular is a PITA as you basically have to clone the element and replace it on the DOM to change anything meaningful on it.

    It's also NOT entirely reliable on the outdated outmoded Netscape 4 style document.forms array, often failing to populate it if your form elements are dynamically created using JavaScript. That's a real head scratcher I've butted heads with more than once as it seems to work, then not work, willy-nilly with no real predictable behavior.

    Which is why again, I would NOT be using the document.forms array OR trying to access form elements by their name attributes in the JavaScript.

    Side note, if you're going to set background-color from .style, don't do the string you are overwriting any native CSS in its entirety! That's why you set style.backgroundColor="#E0E0E0";

    #E0E0E0, I think I wanna know ya, know ya, woah-oh, that jungle love, yeah #E0E0E0...

    Did we ever mention what a pain in the ass supporting JScript (IE's clone of JavaScript that isn't QUITE JavaScript) is?

    Again I'd be interested to see the FORM... that way PROPER code for whatever it is you're doing could be written since without that, any scripting presented is just wild guesses and possibly outright gibberish.
     
    deathshadow, Jun 15, 2018 IP
  12. clay5366

    clay5366 Member

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #12
    I attached the page, but changed the extension to txt. It is actually asp.

    The page is created using FrontPage.

    Thanks
    David
     

    Attached Files:

    clay5366, Jun 18, 2018 at 9:16 AM IP
  13. deathshadow

    deathshadow Acclaimed Member

    Messages:
    8,581
    Likes Received:
    1,535
    Best Answers:
    223
    Trophy Points:
    515
    #13
    Well there's your problem...

    Seriously, just stop. Your markup is TWO DECADES out of date, you've got endless pointless JavaScript doing HTML and CSS' job, nothing remotely resembling semantic markup, there's tables for layout, presentational markup, no doctype, an outdated incompatible character encoding...

    The ONLY thing you can can learn from Frontpage is how NOT to build a website. MORE so now that it's been dead and buried by its creators for nearly a DECADE.

    ... which confirms my suspicion, you're trying to throw JavaScript at something that has ZERO business being controlled from the JavaScript. This is NOT how you are supposed to use HTML, CSS, or JS. AT ALL. The whole mess needs to be tossed in the trash and started over -- ANYONE telling you otherwise is blowing smoke up your backside.
     
    deathshadow, Jun 18, 2018 at 10:20 AM IP
  14. clay5366

    clay5366 Member

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #14
    I getcha. Not a programmer. Only dabble. Much easier to maintain data thru a DB rather than endless paper. I knew Access and figured out how to access the DB thru FP.

    Because of the DB connections, I have never looked any further.
     
    clay5366, Jun 18, 2018 at 11:34 AM IP
  15. clay5366

    clay5366 Member

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #15
    Hello DeathShadow,

    Hoping you you drop this here. Can you suggest an alternative to Frontpage? I also need to consider having the ability to create database connections.

    I am certainly open to suggestions. This is only 1% of my job here, and dealing with web issues internally is very few and far between.

    Thanks
    David
     
    clay5366, Jun 19, 2018 at 10:30 AM IP