Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    New Coder
    Join Date
    Dec 2010
    Posts
    27
    Thanks
    1
    Thanked 0 Times in 0 Posts

    attach onclick event to default dropdownlist value

    i have a dropdownlist with some values,ex: One, Two and Three; One being the default value selected on load of dropdown list.

    When I select Two or Three, there is onselectedchange event fired and I can write custom code in handler. But I also want to do some custom handling when page is loaded and user justs clicks on default value of "One". I want to take an action when user clicks on default value, "One" of dropdwn.

    any suggestions/ideas?

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,965
    Thanks
    79
    Thanked 4,429 Times in 4,394 Posts
    Try using onblur instead of onchange, see if that works.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Quote Originally Posted by Old Pedant View Post
    Try using onblur instead of onchange, see if that works.
    That would only work when after the onblur the user clicks elsewhere on the page and moves the focus, and of course triggers the alert regardless of which option has been focused on. You cannot attach an onblur or onclick event to an individual select box option.

    Code:
    <form name='myform'>
    <select name = 'list1' id = 'list1' onblur = "chk()">
    <option selected value=""> Choose A Fruit</option>
    <option value='Mango'> Mango </option>
    <option value='Apple'> Apple </option>
    
    </select>
    
    <script type = "text/javascript">
    function chk() {
    alert ("Hello")
    }
    </script>
    Why should the user click on the selected value of the option list if that is the option he desires? That is the whole point of setting an option to selected, i.e. the default. Why do you not simply get the value of the select list?

    In other words, make the default option something like "Choose Something....." as above so the user is obliged to select an option.



    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.
    Last edited by Philip M; 12-22-2010 at 10:05 AM.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,965
    Thanks
    79
    Thanked 4,429 Times in 4,394 Posts
    Well, I was directly answering his requirement:
    I want to take an action when user clicks on default value, "One" of dropdwn.
    Yes, if the user never clicks on the <select>, at all, nothing will be triggered.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    Try this out.

    NOTE: Untested!

    Code:
    <form name="carForm">
      <select name="car" id="car" onblur="chkVal(this)">
        <option value="" selected="selected">Choose A Car</option>
        <option value="Ford">Ford</option>
        <option value="BMW">BMW</option>
      </select>
    </form>
    <script type = "text/javascript">
    function chkVal(o) {
      if(o.options[o.selectedIndex].defaultSelected) {
        alert("Default was selected");
      }
    }
    </script>
    Last edited by DJCMBear; 12-22-2010 at 08:40 PM.
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,965
    Thanks
    79
    Thanked 4,429 Times in 4,394 Posts
    HUH???

    Options don't have a defaultValue. Did you mean defaultSelected???
    Code:
      if(o.options[o.selectedIndex].defaultSelected) {
         ... still on default option ...
      }
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    Ahh yes thanks Old Pedant I think I was thinking of something else while I wrote it, I will change the code now.
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Again, that would only work when after the onblur the user clicks elsewhere on the page and moves the focus.

  • #9
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    Onchange triggers once the user has selected a different option with-in the selectbox and onblur will trigger if the user clicks on the select box to make it drop down then clicks somewhere else on the page so if you combined the two, so having both onchange and onblur wouldn't that make it trigger the even if the user clicks a different option or clicks anywhere else on the page?
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P

  • #10
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    A bit like this.

    Code:
    <form name="carForm">
      <select name="car" id="carSelecter">
        <option value="" selected="selected">Choose A Car</option>
        <option value="Ford">Ford</option>
        <option value="BMW">BMW</option>
      </select>
    </form>
    <script type = "text/javascript">
    function chkVal(o) {
      if(o.options[o.selectedIndex].defaultSelected) {
        alert("Default was selected");
      }
    }
    
    window.onload = (function(){
      var CS = document.getElementById('carSelecter');
      if(CS) {
        CS.onchange = CS.onblur = (function(){
           return chkVal(this);
        });
      }
    });
    </script>
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P

  • #11
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    No difference. The alert is triggered only when the focus is moved by clicking elsewhere on the page.

    The whole thing seems to me to be flawed. It is not expected behaviour for the user to click on an option, as opposed to selecting it in the usual way. Again, why should the user click on the default option?

  • #12
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    I think what the OP is asking for is if the user clicks to open the dropdown but then doesn't change the selected option and clicks somewhere else to try and run a function to maybe stop the user from doing anything else unless they change the selected option, a bit like once you open it you have to select an option. Or that's what my guess was when reading the OPs post.
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P

  • #13
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Quote Originally Posted by DJCMBear View Post
    I think what the OP is asking for is if the user clicks to open the dropdown but then doesn't change the selected option and clicks somewhere else to try and run a function to maybe stop the user from doing anything else unless they change the selected option, a bit like once you open it you have to select an option. Or that's what my guess was when reading the OPs post.
    Well the proper way is (as I say) to have a default

    <option value = "">Make a choice....</option>.

  • #14
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    That's how I would do it, plus also use selected="selected" but sometimes developers are trying to do something that I personally wouldn't do but if they need it I see it as a chance to help someone out.
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P

  • #15
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Quote Originally Posted by DJCMBear View Post
    That's how I would do it, plus also use selected="selected" but sometimes developers are trying to do something that I personally wouldn't do but if they need it I see it as a chance to help someone out.
    Some developers are too inexperienced to recognise a bad idea, and it is not in their best interests to encourage them. How can the OP "need" this?


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •