www.webdeveloper.com
+ Reply to Thread
Results 1 to 2 of 2
  1. #1
    Join Date
    Sep 2006
    Location
    India
    Posts
    584

    Question Getting selected dropdown text

    Hi

    I have 5 dropdowns on my webpage. What I want is when any of the options from any dropdowns are selected, i should be able to alert the text of it (not the value). I am trying the following code but it does not display any result.


    Code:
    $(document).ready(function(){
    
        $('select').change(function(){
    
             alert( $(this + '  option:selected' ).text() );
    
        });
    
    });

    Pls help

  2. #2
    Join Date
    Sep 2007
    Posts
    275
    I try:
    alert($('select:eq('+$(this).index()+') option:selected').html());
    alert($('select:eq('+$(this).index()+') option:selected').text());
    alert( $($(this+ 'option:selected')[$(this).index()]).text() );

    Code:
      
    <html>
    <head>
    <title></title>
    <meta charset="utf-8">
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
    <script type="text/javascript">
    // http://www.webdeveloper.com/forum/showthread.php?t=256214
    // http://www.w3schools.com/jquery/jquery_ref_selectors.asp
    
    $(document).ready(function(){
    
        $('select').change(function(){
    // alert( $('select').length); // 2
    
    //var s = $('select option:selected');
    /*
    alert( $(this + ':selected').text());
    alert( $($(':selected')[$(this).index()]).text());
    */
    /*
    var s = $(this + 'option:selected');
    alert(s);
    alert(s.length);
    alert(s.val());
    alert(s.text()); 
    var ind= $(this).index();
    alert(ind);
    var el = s[ind];
    var txt = $(el).text();
    alert (txt);
    
    */
    
    //alert($('select:eq('+$(this).index()+') option:selected').html());
    //alert($('select:eq('+$(this).index()+') option:selected').text());
    
    alert( $($(this+ 'option:selected')[$(this).index()]).text() );
    //alert( $($(this+ 'option:selected')[$(this).index()]).html() );
    
    });
    });
    
    </script>
    </head>
    <body>
    <select>
    <option value="">L&#252;tfen bir tanesini se&#231;in!</option>
    <option value="elhamd&#252;lillah">&#246;vg&#252; Allah'a mahsustur</option>
    <option value="subhanallah">Allah, noksan sıfatlardan m&#252;nezzehtir</option>
    <option value="allah&#252;ekber">Allah, b&#252;y&#252;kt&#252;r</option>
    </select>
    
    <select>
    <option value="">L&#252;tfen bir tanesini se&#231;in!</option>
    <option value="la ilahe illallah">Allah'tan başka ilah yoktur</option>
    <option value="maşallah">Allah ne g&#252;zel yaratmış</option>
    <option value="inşallah">Allah'ın izniyle</option>
    </select>
    </body>
    </html>
    Last edited by Ayşe; 02-01-2012 at 06:34 AM.
    The Time Through Ages
    In the Name of Allah, Most Gracious, Most Merciful
    1. By the Time,
    2. Verily Man is in loss,
    3. Except such as have Faith, and do righteous deeds, and (join together) in the mutual enjoining of Truth, and of Patience and Constancy.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Bookmarks

Posting Permissions

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



Recent Articles