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.
Results 1 to 14 of 14
  1. #1
    Regular Coder
    Join Date
    May 2005
    Posts
    262
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Help accessing value

    Hello, I am using AJAX to load a control into 2 different spans on a page. I need to know how to get the selected values

    I am calling a JS function onclick of the button

    Code:
    function get_droplist_users()
    {
    //need to know how to access each of the values here. An alert would suffice
    
    }

    Here is the HTML
    Code:
    <form name="frm_generic">....
    
    
    <tr>
    <td valign="top">						
    <span id="reg_users"><select name="drp_users"  onchange="createCookie( 'ActiveUser', this.options[ this.selectedIndex ].value );">
    <option value="">-- Select a User --</option>
    <option value="2">Glenn</option>
    <option value="4">Jef</option>
    <option value="3" selected="selected">Tim</option>
    </select>	
    </span>	
    </td>
    <td>
    <span id="reg_users2"><select name="drp_users"  onchange="createCookie( 'ActiveUser', this.options[ this.selectedIndex ].value );">
    <option value="">-- Select a User --</option>
    <option value="2" selected="selected">Glenn</option>
    <option value="4">Jef</option>
    <option value="3">Tim</option>
    </select>	
    </span>
    </td>
    </tr>
    <tr>
    <td>
    <input class="btn_Save" name="btn_Save" id="btn_Save" onclick="get_droplist_users();" value="Save" type="button">		
    </td>
    </tr>
    If you can give me a way to alert each of the values of the 2 droplists in my function onclick of the button that would rock. I have tried a few things but really have no idea how to do this using two fields of the same name...I was hoping I could get to the value through the different IDs in the spans (reg_users and reg_users2)

    Normally I only use this control once on a page and just access the cookie I set, but since I am naming the cookie inside the control itself, I am basically over riding the cookie before it. SO I just want to access the value instead of the cookie in this instance.

    Thank you for any help with this
    Last edited by tripwater; 12-13-2007 at 10:33 PM. Reason: removed the ids from the droplists to eliminate confusion

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Code:
    function get_droplist_users()
    {
    //need to know how to access each of the values here. An alert would suffice
    var s = document.getElementById('drp_users'); // the attribut id of your select element
    // s.value <= attribute value of selected option element
    alert(s.value);
    }
    the attribut id of any node in a page, if is defined, must be uniq.

    best regards
    Last edited by oesxyl; 12-13-2007 at 09:43 PM. Reason: usualy missing of "best regards", :-)

  • #3
    Regular Coder
    Join Date
    May 2005
    Posts
    262
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Thank you for the response. I actually need to remove that id from the drop list.

    See that is a single control, in an external file that I pull in via AJAX.

    Is there a way to get the values of the drop of the same name? See it is called drp_users but since I call it in twice on the same page, I have 2 selects on the page of the same name BUT they are loaded in spans with different IDs. Is it possible to get access to the 2 different values through the unique IDs of the spans?

    Something like

    document.getElementById( 'reg_users' ).drp_users.value

    and

    document.getElementById( 'reg_users2' ).drp_users.value

    Nevermind the id of the drop list, I am going to remove that.

    Remember this drop list of users is a single control loaded into the page twice.

    Thanks
    Last edited by tripwater; 12-13-2007 at 10:01 PM.

  • #4
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by tripwater View Post
    Thank you for the response. I actually need to remove those ids from the drp list.

    See that is a single control, in an external file that I pull in via AJAX.

    Is there a way to get the values of the drop of the same name? See it is called drp_users but since I call it in twice on the same page, I have 2 selects on the page of the same name BUT they are loaded in spans with different IDs. Is it possible to get access to the 2 different values through the unique IDs of the spans?

    Something like
    if you use getElementById the name attribut is unimportant, but the id is.


    Quote Originally Posted by tripwater View Post
    document.getElementById( 'reg_users' ).drp_users.value

    and

    document.getElementById( 'reg_users2' ).drp_users.value
    Code:
    var val1 = document.getElementById( 'reg_users' ).value
    var val2 = document.getElementById( 'reg_users2' ).value
    'red_uesrs' is the id of first select and 'reg_users2' of second.

    as I say you don't need to use name attribute

    best regards

  • #5
    Regular Coder
    Join Date
    May 2005
    Posts
    262
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Ok so here is what I tried

    Code:
    function get_droplist_users()
    {
    alert( document.getElementById( 'reg_users2' ).value );
    }

    And the alert says undefined. I want to repeat that the reg_users ID is not that of the droplist but that of the span the droplist is inside of. Just want to be sure you understand this as I did not know you could get the value of a field inside of a span by just saying the span-id.value which is what we are doing here...

    Please be patient, I an still learning

    And I still think you are confused because you said this
    'red_uesrs' is the id of first select and 'reg_users2' of second.
    The reg_users and reg_users2 are the ids of the SPANS the drop lists are in, not the droplists themselves. Please review the code example above. Again, the droplist is a single droplist that is from an external file that I bring in 2X via AJAX call in these spans.
    Last edited by tripwater; 12-13-2007 at 10:32 PM. Reason: Formating the code better

  • #6
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    [QUOTE=tripwater;639357]Ok so here is what I tried

    Code:
    function get_droplist_users()
    {
    alert( document.getElementById( 'reg_users2' ).value );
    }

    And the alert says undefined. I want to repeat that the reg_users ID is not that of the droplist but that of the span the droplist is inside of. Just want to be sure you understand this as I did not know you could get the value of a field inside of a span by just saying the span-id.value which is what we are doing here...[quote]

    sorry, my mistake. you must use the id of the element select not span.

    Code:
    <input type="text" id="thisid" value=""/>
    document.getElementById('thisid') is the object, never mind if is input
    or select, is identified using the id attribute 'thisid'.
    After you identify the object you can retrive his value using value as we
    both say.

    Quote Originally Posted by tripwater View Post

    Please be patient, I an still learning
    no problem,

    Quote Originally Posted by tripwater View Post
    And I still think you are confused because you said this

    The reg_users and reg_users2 are the ids of the SPANS the drop lists are in, not the droplists themselves. Please review the code example above. Again, the droplist is a single droplist that is from an external file that I bring in 2X via AJAX call in these spans.
    you are right,

    I hope that now my explanation is less confused.

    best regards.

  • #7
    Regular Coder
    Join Date
    May 2005
    Posts
    262
    Thanks
    4
    Thanked 0 Times in 0 Posts
    THank you again.

    But I already knew how to access the value of a field or select from the id of the field. The problem in this situation is

    1. I am bringing in a select from an external page and it has a name of drp_users and I am loading it into the span with the ID of reg_users
    2. I am bringing the same select from the same external page and it has a same name of drp_users and I am loading it into the span with the ID of reg_users2

    I cannot give the select an ID because it is a control that I use through the site in many places. Some times (like this situation) I am loading the control twice. So this means that I would have the same select on the page twice and both would have the same ID because the select is from a single file being loaded twice.

    So to remedy this, I was hoping to get to the value of EACH SELECT through the SPAN IDs that are unique.

    So to recap,

    I have a page that loads a select called "drp_users" 2 times from an external source via AJAX call and loads each of them into a separate span that has a unique ID. I want to know how and if possible, I can select something from each droplist, hit the button and it tell me the values selected BASED on the spans they are in. I need it this way because, again, it is a single control loaded into the page twice. So if I go to the page that has the droplist and give it an ID, then when I call my select twice, both will have the ID since it is calling the same file. I feel like I am overly explaining this but feel like I have to at this point because my point is somehow getting lost.

  • #8
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by tripwater View Post
    THank you again.
    sorry again.

    my javascript knowledge are somehow limited and once I understand the problem ( read this as "I read carefully the thread", ) I try to figure out how you can solve it. I find this:

    somewhere before the form include:

    Code:
    var selarray = []
    var spanind = 1;
    function checkCookie(u, s){
      salarray[spanind] = s;
      spanind++;
    }

    and after form:

    Code:
    function get_droplist_users(){
      // do what you need with salarray
      val = '';
      for(i in salarray){
         val = val + i + ' ' + salarray[i];
      }
      alert(val);
    }
    I test this only with iceweaseal/fireforx and I don't know if works in ie.
    Maybe somebody with more knowledge then me, could provide a better solution.

    best regards.

  • #9
    New Coder
    Join Date
    Jun 2005
    Posts
    52
    Thanks
    0
    Thanked 4 Times in 4 Posts
    try...

    alert(document.getElementById('reg_users').getElementsByName('drp_users')[0].value);
    alert(document.getElementById('reg_users2').getElementsByName('drp_users')[0].value);

  • #10
    Regular Coder
    Join Date
    May 2005
    Posts
    262
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Zefris View Post
    try...

    alert(document.getElementById('reg_users').getElementsByName('drp_users')[0].value);
    alert(document.getElementById('reg_users2').getElementsByName('drp_users')[0].value);

    Thank you both for the replies. I have yet to try your solution oesxyl. This one by Zefris is more along the lines of what I was hoping for in terms of simplicity. I plugged it into my code and I got this error

    document.getElementById("reg_users").getElementsByName is not a function
    I will try your solution now, oesxyl. Thank you again for the replies. If anyone or Zefris comes back and finds a way to make his/her post work I would prefer that type of solution.

  • #11
    Regular Coder
    Join Date
    May 2005
    Posts
    262
    Thanks
    4
    Thanked 0 Times in 0 Posts
    I just tried this also

    Code:
     alert(document.getElementById('reg_users').getElementsByTagName('select').value);
    alert(document.getElementById('reg_users2').getElementsByTagName('select').value);
    and

    Code:
     alert(document.getElementById('reg_users').getElementsByTagName('select').selectedIndex);
    alert(document.getElementById('reg_users2').getElementsByTagName('select').selectedIndex);
    Both alerted undefined...
    Last edited by tripwater; 12-14-2007 at 02:21 PM.

  • #12
    Regular Coder
    Join Date
    May 2005
    Posts
    262
    Thanks
    4
    Thanked 0 Times in 0 Posts
    I figured out my problem and this is how I resolved it. Thanks again for your replies and suggestions.

    Code:
    function get_droplist_users()
         24    {
         25    var elem = document.forms[ 'frm_generic' ].elements;
         26
         27    var userfrom   = elem[ 0 ];
         28    var userto     = elem[ 1 ];
         29
         30    alert(userfrom.options[userfrom.selectedIndex].value);
         31
         32    alert(userto.options[userto.selectedIndex].value);
         33    }
    I just said screw getting to it by id and got to each by their element array

  • #13
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by tripwater View Post
    I will try your solution now, oesxyl. Thank you again for the replies. If anyone or Zefris comes back and finds a way to make his/her post work I would prefer that type of solution.
    I don't test it with IE.
    In case you find another way to solve this problem and you don't use checkCookie function, redefine it somewhere before form to avoid error messages. And please post it here, some people including me could
    find it usefull.

    Code:
    function checkCookie(u, s) {} ;
    best regards

  • #14
    New Coder
    Join Date
    Jun 2005
    Posts
    52
    Thanks
    0
    Thanked 4 Times in 4 Posts
    i remember this not working in ie...

    alert(document.getElementById('reg_users').getElementsByName('drp_users')[0].value);
    alert(document.getElementById('reg_users2').getElementsByName('drp_users')[0].value);

    you can also try... but this will only work with values directly after #reg_users/#reg_users2...

    alert(document.getElementById('reg_users').childNodes[0].value);
    alert(document.getElementById('reg_users2').childNodes[0].value);
    Last edited by Zefris; 12-14-2007 at 04:38 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
    •