+ Reply to Thread
Results 1 to 8 of 8

Thread: Javascript on change event

  1. #1
    warlordste's Avatar
    warlordste is offline x10 Elder warlordste is an unknown quantity at this point
    Join Date
    Sep 2007
    Location
    Wigan
    Posts
    653

    Javascript on change event

    Hi guys how you all doing i was wondering if anyone could give me abit of help I have a form for a quote system where the person can request for the item to be delivered or picked up which i have set in a list box with yes and no as the values i was wondering how can i get it so if it changes to yes the javascript show the options for delivery address and if it changes to no it hides this infomation?
    "It's time to prove to your friends that you're worth a damn. Sometimes that means dying, sometimes it means killing a whole lot of people."- sin city "You either die a hero or you live long enough to see yourself become the villain" - TDK



  2. #2
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Javascript on change event

    Firstly, a checkbox (<input type="check" .../>) is more natural than a select when the options are yes/no. Second, if you're asking how to show or hide an element, you can set the element's display to 'none' or (better yet) create a class that has a display of "none". To hide an element, give it the hidden class; to show it, remove the class. Note that if you're using a JS lib (such as jQuery, Prototype or MooTools), it should have functions that hide and show elements. Such libraries offer many benefits, such as animating hiding or showing of the element (sliding into place is less unsettling than simply appearing and disappearing, hence a better user experience).
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  3. #3
    xpsystemservices64 is offline x10Hosting Member xpsystemservices64 is an unknown quantity at this point
    Join Date
    Sep 2010
    Posts
    2

    Re: Javascript on change event

    The "onchange" event is calling when control loosing the focus. Use "onclick" instead "onchange".

  4. #4
    lemon-tree's Avatar
    lemon-tree is offline x10 Minion lemon-tree has a spectacular aura about
    Join Date
    Nov 2007
    Posts
    1,420

    Re: Javascript on change event

    Actually, even if it is calling on loss of focus (Although I don't think it should be), you should still use onchange and then within the called function check what the state of the checkbox is using the .checked property of the element. You should never just assume that because it has been clicked that it is now checked, so you should always include an actual verification of the element. Here is a very crude example that would work but isn't technically the best way:
    HTML Code:
    <html>
    	<head>
    		<script type='text/javascript'>
    			function checkChange(element){
    				if(element.checked === true){
    					//THE ELEMENT IS CHECKED
    					document.getElementById('customerAddress').style.display = 'none';
    				}else{
    					//THE ELEMENT IS NOT CHECKED
    					document.getElementById('customerAddress').style.display = 'block';
    				}
    			}
    		</script>
    	</head>
    	<body>
    		<label><input type='checkbox' onchange='checkChange(this)'/>Collect?</label>
    		<div id='customerAddress' style='display: block'>
    			123, This Street<br/>
    			That Town</br>
    			Etc
    		</div>
    	</body>
    </html>
    You can test this script out here.

    Edit: Just checked in Safari and the onchange event definitely isn't being called on loss of focus, so something is up with your browser. I could check a whole bunch more, but I don't feel it would be worth the effort.

    Edit 2: Altered the code to show how you could use it.
    Last edited by lemon-tree; 10-27-2010 at 08:05 AM.

  5. #5
    essellar's Avatar
    essellar is offline Community Advocate essellar has a spectacular aura about
    Join Date
    Feb 2010
    Location
    Toronto, Ontario, CA
    Posts
    1,153

    Re: Javascript on change event

    Quote Originally Posted by misson View Post
    Firstly, a checkbox (<input type="check" .../>) is more natural than a select when the options are yes/no.
    I'm going to assume their was a small stroke in progress as you typed this (because I know for a fact that you're not an idiot).

    For those who don't know better: a radio button set is what you'd actually want under these circumstances.

    HTML Code:
    <input type="radio" name="fieldName" value="Yes" id="fieldName_Yes" /><label for="fieldName_Yes">Yes</label>
    <input type="radio" name="fieldName" value="No" id="fieldName_No" /><label for="fieldName_No">No</label>
    Why? By convention, a radio button set must always have exactly one choice selected from the list. A checkbox set, on the other hand, will always allow zero or more items to be selected. Although you can enforce an "at least x choices" or a "not more than x choices" policy during validation of a checkbox, there's never a guarantee that anything is selected, or that mutually-exclusive selections cannot be made at any point in time. Mutually-exclusive selections from a small list of choices (say a half-dozen at maximum) should almost always be represented by a radio button set.

    As for when the onchange event fires, it depends on the widget. Radios, checkboxes and selects fire an onchange immediately when they change value. Text and textareas won't do that -- it would be silly to fire an onchange with every keystroke while a user is typing away madly -- the onchnge event fires when the user is finished with the field, and that is indicated by moving focus out of the field (onchange normally fires immediately before onblur).
    “Beware of bugs in the above code; I have only proved it correct, not tried it.” --Donald Knuth
    "It was as if its architects were given a perfectly good hammer and gleefully replied, 'neat! With this hammer, we can build a tool that can pound in nails.'" -- Alex Papadimoulis (on TheDailyWTF.com)

  6. #6
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: Javascript on change event

    Quote Originally Posted by essellar View Post
    I'm going to assume their was a small stroke in progress as you typed this (because I know for a fact that you're not an idiot).

    For those who don't know better: a radio button set is what you'd actually want under these circumstances.
    Wrong. Not when the choices are Yes/No or Off/On.

    If you have two choices of say, 'Oatmeal' and 'Bacon and Eggs', then radio button set would be better

    It's not 100% clear of what OP's 'options' would be.
    Last edited by descalzo; 10-27-2010 at 04:10 PM.
    Nothing is always absolutely so.

  7. #7
    lemon-tree's Avatar
    lemon-tree is offline x10 Minion lemon-tree has a spectacular aura about
    Join Date
    Nov 2007
    Posts
    1,420

    Re: Javascript on change event

    It ultimately depends upon how he wants to word this:

    • If it is worded, 'Would you like to collect from the warehouse?' then a checkbox may be more suitable.
    • If it is worded, 'How would you like to receive the product?' then radio buttons with 'Collect' and 'Delivery' would make more sense.

    There is no definitive right or wrong answer in this situation and it is really up to the taste of the user and how they want the interaction to take place. You could probably find some UI guide that would give you a suggestion, but those are talking in a very general case and don't really help with specifics.
    Last edited by lemon-tree; 10-27-2010 at 04:33 PM.

  8. #8
    warlordste's Avatar
    warlordste is offline x10 Elder warlordste is an unknown quantity at this point
    Join Date
    Sep 2007
    Location
    Wigan
    Posts
    653

    Re: Javascript on change event

    Hi guys this is what i have so far in my code and its this i want to hide inless the box is selected
    HTML Code:
      <p>How would you like to receive the product?:
      <select name="delivered">
      <option value="sorted">Delivery</option>
      <option value="no">Collect</option>
      </select>
      </p>
      <p>
      <label>Delivery Price
      <input type="text" name="deliveryprice" />
      </label>
      <label>Delivery Quantity
      <input type="text" name="deliveryquantity" />
     </label>
    <label>Delivery First Name
    <input type="text" name="deliveryname" />
     </label>
           <br />
            <br/>
      <label>Delivery Surname
         <input type="text" name="deliverysurname" />
                                                          </label>
                                                        </p>
                                                        Delivery Address Line 1:
    <input name="deliveryadd1" type="text" id="deliveryadd1" />                          
    <br />
                              <br />
                              Delivery Address Line 2:
                              <input name="deliveryadd2" type="text" id="deliveryadd2" />
                              <br />
                              <br />
                              Town:
                              <input name="deliverytown" type="text" id="town" />
                              <br />
                              <br />
                              Postcode:
                              <input name="deliverypostcode" type="text" id="postcode" />
                              <br>                                                 </div>
                              </label>
    						                          <div align="center">
    						                          <div align="center"><br />
    "It's time to prove to your friends that you're worth a damn. Sometimes that means dying, sometimes it means killing a whole lot of people."- sin city "You either die a hero or you live long enough to see yourself become the villain" - TDK



+ Reply to Thread

Similar Threads

  1. Change background color with JavaScript?
    By gaptrast in forum Programming Help
    Replies: 10
    Last Post: 09-08-2010, 02:55 AM
  2. Javascript change style attribute of span
    By diabolo in forum Programming Help
    Replies: 12
    Last Post: 04-28-2010, 04:31 PM
  3. Event Managment
    By jjuder in forum Free Hosting
    Replies: 1
    Last Post: 12-10-2009, 08:10 AM
  4. javascript text class change for heading
    By jakeselectronics in forum Programming Help
    Replies: 2
    Last Post: 10-17-2009, 12:31 PM
  5. Javascript to change iframe url
    By thezone1 in forum Programming Help
    Replies: 3
    Last Post: 04-25-2008, 03:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers