CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   JavaScript programming (http://www.codingforums.com/forumdisplay.php?f=2)
-   -   Javascript to target specific form name? (http://www.codingforums.com/showthread.php?t=283832)

DSchragg 12-09-2012 04:26 AM

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!

Philip M 12-09-2012 11:03 AM

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.

DSchragg 12-09-2012 02:50 PM

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

AndrewGSW 12-09-2012 02:55 PM

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.

DSchragg 12-10-2012 02:15 AM

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!


All times are GMT +1. The time now is 07:00 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.