...

View Full Version : Javascript to target specific form name?



DSchragg
12-09-2012, 05:26 AM
I have a form that loads with the following hidden field:

<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:


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, 12:03 PM
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, 03: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, 03:55 PM
If "PackageID" is, indeed, the ID of the element then you need to use


document.getElementById("PackageID")
to refer to it - as Philip advised you!

document.getElementsByName("SomeName")
returns an array of elements, the first of which is referred to by

document.getElementsByName("SomeName")[0]

What does your Console say? It probably confirms the above with an error message.

DSchragg
12-10-2012, 03: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
$("#field_price_current_wrapper").hide(); isn't working for me, what am I doing wrong?

Thanks again!

EDIT: Managed to hide the div using:

field_price_current_wrapper

Thanks again for all your help guys!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum