| | #1 |
| HyperActive Warrior Join Date: 2009
Posts: 212
Thanks: 11
Thanked 5 Times in 5 Posts
|
I'm trying to get the javascript working on my form to hide or show an input depending on the selection of a drop down.. HTML Code: <label> What Are You Looking To Finance :<select name="item" required=""> <option value="snowmobile">Snowmobile</option> <option value="automobile">Car/Truck/Suv</option> <option value="boat">Boat/Marine</option> <option value="atv">ATV, Side by Side, Off-Road</option> <option value="trailer">Recreationa/Trailer/RV</option> <option value="computer">Electronic/TV/Computer</option> <option value="other">Other</option> </select> </label> <div id="itemother"> <label> Please Describe Item : <input type="text" name="itemother" placeholder="Enter Description and Value" /> </label> </div> Code: $('select[name=item]').change(function(){
if ($('select[name=item]').val() == '7'){
$('#itemother').show();
}else{
$('#itemother').hide();
}
}); HTML Code: #itemother {
display: none;
} |
| |
| | #2 |
| Conceited Know-It-All Join Date: 2014 Location: Amurrica
Posts: 952
Thanks: 465
Thanked 1,136 Times in 503 Posts
|
Two potential issues:
Thus, your javascript should be something like: Code: $('select').change(function(){
if ($('select[name=item]').val() == 'other'){
$('#itemother').show();
}else{
$('#itemother').hide();
}
}); https://jsfiddle.net/8b58hu4k/#&togetherjs=xKTnLJnTmy |
| | |
| |
| | #3 |
| HyperActive Warrior Join Date: 2009
Posts: 212
Thanks: 11
Thanked 5 Times in 5 Posts
|
Ok so I loaded the jquery using the google api in my header - I added your code and it don't work on my site for some reason i'm using wordpress plugin css+javascript to add the java code could this be the issue?
|
| |
| | #4 |
| Conceited Know-It-All Join Date: 2014 Location: Amurrica
Posts: 952
Thanks: 465
Thanked 1,136 Times in 503 Posts
|
I really don't have much experience with that plugin, but your issue could be caused by a million things. And really at this point, without seeing your site I think you're going to have to figure it out yourself. But here are a couple of things I'd try: First, make sure that you're making good use of your Javascript console. If you're not, check out Using the browser console | WickedlySmart and/or Mastering The Developer Tools Console - Treehouse Blog to learn some of the basics on how to use it. Once you're using the console, the first thing to do is to check for any errors. If you've got bad JavaScript somewhere else, it could be preventing this code from running too. Second, forget your own code for a second. Check to see if you're able to get any JavaScript to run using that plugin. Try running some code like this using your plugin: Code: alert("It works!"); If not, it's likely that your plugin isn't doing what you think it should. Or maybe you have another JavaScript error that's preventing this code from running (though if your console isn't showing any errors, that shouldn't be the case.) At this point, I'd "View Source" on my page to make sure my code is being written the way it should be by the plugin. If you did get an alert message, then perhaps the problem is with JQuery. Remove your original alert and put this there instead: Code: $(document).ready(function(){alert('jQuery Works!')}); What I'm trying to show you is how you might approach debugging your problem. At the beginning literally anything could be causing your issue. Your job is to narrow it down logically. Which is why first, I wanted to make sure that there were no other JavaScript issues that could be interfering with your code (check the console!) Second, I wanted to make sure that JavaScript was actually running in your plugin. Third I wanted to make sure that jQuery was actually working in your plugin. You may have to think of the fourth and fifth steps based on what you find as you start debugging. Good luck! |
| | |
| |
| | #5 |
| Bananas are Yellow! Registered Member War Room Member Join Date: 2013 Location: Wyoming, PA, USA
Posts: 1,046
Thanks: 2,549
Thanked 1,600 Times in 928 Posts
Blog Entries: 4 |
Strictly a guess here, but try replacing select[name=item] with #item (twice).
|
| Put MY voice on YOUR video: AwesomeAmericanAudio.com | |
| |
| | #6 |
| Active Warrior Join Date: 2015
Posts: 67
Thanks: 27
Thanked 9 Times in 8 Posts
|
You can use Jquery !
|
| |
| | #7 |
| Bananas are Yellow! Registered Member War Room Member Join Date: 2013 Location: Wyoming, PA, USA
Posts: 1,046
Thanks: 2,549
Thanked 1,600 Times in 928 Posts
Blog Entries: 4 | |
| Put MY voice on YOUR video: AwesomeAmericanAudio.com | |
| |
| | #8 |
| Antone Roundy War Room Member Join Date: 2005 Location: Grand Island, NE, USA
Posts: 56
Thanks: 2
Thanked 30 Times in 16 Posts
|
I'm not a JQuery user, so I can't comment on that. But this is a simple enough thing to do in straight JavaScript with just a few little changes from what you've done. Code: ...<select name="item" required="" onChange="SelectChanged(this)">... Code: function SelectChanged(el) {
document.getElementById('itemother').style.display=(el.options[el.selectedIndex].value=='other')?'block':'none';
} |
|
Antone Roundy Want up to 57 bloggers to support your blog? Join a Team (free). Keep your blog active faster and easier with "Blog Riffing": * Free: PDF, High Impact Blog Riffing Course | |
| |
|
| Bookmarks |
| Tags |
| javascript |
| |