monkeytooth Posted December 19, 2009 Share Posted December 19, 2009 I have search everywhere for this, even if its so much as a little example to work with/off of, but I am starting to think its not possible. I have found ways to get the values via the use of jquery. But is there a way to set them. What I have and am tempting to do is build a simple form. User puts in their billing address, and goes onto the shipping address. I want to offer them the ability to tick off a check box and auto populate the shipping data fields if the user deems them as one in the same. Which that part I got no problem. Its the select boxes for the states, countries, and address type (apartment, house, dorm...) I can't figure out how to make the select inputs go from default to a matching selected option. Does anyone know how to do this? Is if possible? Am I going about this with the wrong logic do I need to do something outside of using jquery to do this? Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted December 19, 2009 Share Posted December 19, 2009 jQuery functions are overloaded so you can use the same function to get and set example $('#element').val(); // gets the value $('#element').val('test'); // sets the value Quote Link to comment Share on other sites More sharing options...
monkeytooth Posted December 19, 2009 Author Share Posted December 19, 2009 Ok.. Thats the route I was attempting to use. However. With the dropdown/select boxes. It doesnt seem to work. Here is an example of what I am doing to try to get the drop down to set. if($j('input#bstate').val() == ""){ alert("State empty."); }else{$j("input#sstate").val($j("input#bstate").val());} That above works on everything thus far except for the drop downs. When ran the drop downs don't jump to the area to set it and make it as if it is the one selected. Which is kinda what i need most. I cant do an append as just in the country box theres 244 selections. Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted December 19, 2009 Share Posted December 19, 2009 use the $("#id option[value=thevalue]") selector so your code should be if($j('input#bstate').val() == ""){ alert("State empty."); }else{$j("input#sstate option[value="+$j("input#bstate").val()+"]").attr('selected', 'selected');} Quote Link to comment Share on other sites More sharing options...
monkeytooth Posted December 19, 2009 Author Share Posted December 19, 2009 doesn't seem to have any effect. Does it matter if by default the there is a "selected" in the HTML already. Its purely cosmetic. "--Select State--" is the default selected value in the HTML. If the user hasn't already set his/her state for example when the page loads up and reads from the DB. If the php finds a certain variable to be set a certain way it tells the drop down to select the above as default. Then again that applies to every row of the drop down. If the user has already come along and set this setting what ever it is they set will be the default selected when the page loads. I was just over looking all aspects of this, and it dawned on me that may or may not be effecting this somehow, I am very new to using jquery in any way form shape or another so I am a bit lost. So far its only been mild speed bumps as I build this project with a slight learning curve on one to many deadlines. But they have all been over come, this one has me stumped. Unfortunately searching else where really hasn't yielded many results on the subject either. Is this something maybe I would have to use jquery to append to the select list a new option? Or is the method your originally saying one that can work. Just posing to give a bit of trouble at the moment. Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted December 19, 2009 Share Posted December 19, 2009 its should work as I tested it out on my test page something must be going wrong here is my test <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title> new document </title> <script src='http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js' type="text/javascript"></script> <script> $(document).ready(function() { $("#selectList option[value=best]").attr('selected', 'selected'); $('#appendoption').click(function() { var html = "<option value='pest'>pest</option>"; $('#selectList').append(html); }); }); </script> </head> <BODY> <select id="selectList"> <option value="test"> test</option> <option value="best"> best</option> </select> <input id="appendoption" type="button" /> </body> </html> for the append jquery makes it easy you can just use the append function to append to test area I have show it in my above example Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.