Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-09-2012, 04:26 AM   PM User | #1
DSchragg
New to the CF scene

 
Join Date: Nov 2012
Posts: 7
Thanks: 3
Thanked 0 Times in 0 Posts
DSchragg is an unknown quantity at this point
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!
DSchragg is offline   Reply With Quote
Old 12-09-2012, 11:03 AM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,037
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
DSchragg (12-10-2012)
Old 12-09-2012, 02:50 PM   PM User | #3
DSchragg
New to the CF scene

 
Join Date: Nov 2012
Posts: 7
Thanks: 3
Thanked 0 Times in 0 Posts
DSchragg is an unknown quantity at this point
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
DSchragg is offline   Reply With Quote
Old 12-09-2012, 02:55 PM   PM User | #4
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
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
AndrewGSW is offline   Reply With Quote
Users who have thanked AndrewGSW for this post:
DSchragg (12-10-2012)
Old 12-10-2012, 02:15 AM   PM User | #5
DSchragg
New to the CF scene

 
Join Date: Nov 2012
Posts: 7
Thanks: 3
Thanked 0 Times in 0 Posts
DSchragg is an unknown quantity at this point
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 02:21 AM.. Reason: solved my own question
DSchragg is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:36 PM.


Advertisement
Log in to turn off these ads.