SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Jun 2008
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Jquery: get select option text help!

    Hi all, I’m trying to get the text() of a selected option that has been dynamically built.

    I am using the following code in a doc ready fun:

    Code javascript:
    $('#content_category').change(function(){
    var catselect = "";
    $('#content_category option:selected').each(function(){
    catselect = $(this).text();
    });
    $("#placeholder_content_sort").html(catselect);
    });
     
    $('#content_sub_category').change(function(){
    var subselect = "";
    $('#content_sub_category option:selected').each(function(){
    subselect = $(this).text();
    });
    $("#placeholder_content_sort").append(subselect);
    });

    The second select (#content_sub_category) is populated dynamically (PHP) onChange of the #content_category select.

    I can get the text() of the #content_category select but nothing for the #content_sub_category select!

    I’ve tried to look into rebinding on Jquery’s FAQ page, but can not get it working.

    I’d really appreciate some help? Thanks in advance.
    Last edited by ianhempsell; Jun 29, 2009 at 10:47.

  2. #2
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not tested but can you try once simply like this?
    Code javascript:
    $('#content_sub_category').change(function(){
    	var subselect = $('#content_sub_category option:selected').text();
    	$("#placeholder_content_sort").append(subselect);
    });
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  3. #3
    SitePoint Member
    Join Date
    Jun 2008
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by rajug View Post
    Not tested but can you try once simply like this?
    Code javascript:
    $('#content_sub_category').change(function(){
    	var subselect = $('#content_sub_category option:selected').text();
    	$("#placeholder_content_sort").append(subselect);
    });
    Thanks Rajug, but this doesn't work.

    If I have a static #content_sub_category select the both the code you suggested and my original code works fine, but once the dynamic #content_sub_category select has been populated, I can not get $('#content_sub_category option:selected').text(); to return anything.

  4. #4
    SitePoint Member
    Join Date
    Jun 2008
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If this helps anyone, this is the code that I use to dynamically populate the #content_sub_content select:

    Code javascript:
    $('#content_category').change(function(){
    $("#placeholder_select_sub_category").hide();
    var select_category = "";
    $('#content_category option:selected').each(function(){
    selected_category = $(this).text();
    });
    $("#placeholder_select_sub_category").load("inc/funcs/select.subcategory.inc.php?category="+selected_category).fadeIn(1500);
    });


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
  •