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 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Nov 2012
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Javascript to target specific form name?

    I have a form that loads with the following hidden field:
    Code:
    <input type="hidden" name="packageID" value="1" />
    This value can be either a 1 or 4 depending on what group you are part of on the site, however the form doesn't change.

    What I want to do is customize the form a bit based on the value using something like this:
    Code:
        var package = document.getElementsByName("packageID"); 
        if(package.value == '1'){
        $("#field_price_current_wrapper").hide();
    
        }else{
    
        if(package.value == '4'){
        }
    }
    I'm loading this in my footer after the form but it just isn't working, I'm hoping someone can tell me where I went wrong?

    Thanks!

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,243
    Thanks
    203
    Thanked 2,555 Times in 2,533 Posts
    Assign an id = "packageID" to the field and then use

    var package = document.getElementsById("packageID");


    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    DSchragg (12-10-2012)

  • #3
    New to the CF scene
    Join Date
    Nov 2012
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts
    I'm not trying to assign a PackageID, that's already done, I want to hide fields and force values depending on whether the PackageID is a 1 or 4...

    Thanks

  • #4
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    If "PackageID" is, indeed, the ID of the element then you need to use

    Code:
    document.getElementById("PackageID")
    to refer to it - as Philip advised you!
    Code:
    document.getElementsByName("SomeName")
    returns an array of elements, the first of which is referred to by
    Code:
    document.getElementsByName("SomeName")[0]
    What does your Console say? It probably confirms the above with an error message.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • Users who have thanked AndrewGSW for this post:

    DSchragg (12-10-2012)

  • #5
    New to the CF scene
    Join Date
    Nov 2012
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Guys, thank you so much for your help, and please excuse my ignorance if I wasn't getting the message you were trying to relay. I wasn't trying to add an ID to the field, nor did I want to since this is a Wordpress plugin, and I'd rather not customize it so that I can have clean updates in the future. It was adding the [0] that the did the trick....now if I could ask one more question?

    How to I hide a div id/class?
    Using
    Code:
    $("#field_price_current_wrapper").hide();
    isn't working for me, what am I doing wrong?

    Thanks again!

    EDIT: Managed to hide the div using:
    Code:
    field_price_current_wrapper
    Thanks again for all your help guys!
    Last edited by DSchragg; 12-10-2012 at 03:21 AM. Reason: solved my own question


  •  

    Posting Permissions

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