0

I have a select field and I'm trying to get the HTML within the selected HTML. It keeps returning as undefined.

I've read answers saying to user the .val() function but the value and the HTML aren't the same so it won't work.

HTML:

<select id="my_SiteUsers" style="width:250px;" onchange="RefreshGroupLists()">
        <option value='default' disabled="disabled">Select a user</option>
</select>

JS:

var userText;
userText = $('#my_SiteUsers').html();

//check if default user selected  alert(user);
  if(user!="default"){
    var removeConfirm = confirm("You are about to delete "+userText+"from SharePoint. Are you sure?");
    if(removeConfirm){
      $.SPServices({
        operation:"RemoveUserFromSite",
        userLoginName: user,
        async:true
      });
    }
  }
}
Batman
  • 5,563
  • 18
  • 79
  • 155

3 Answers3

2

So what you need is actually text()

var userText = $("#my_SiteUsers option:selected").text();

html() will return undefined because there is actually no HTML inside the option element.

Sébastien
  • 11,860
  • 11
  • 58
  • 78
1

I think you have to use

$("#my_SiteUsers option:selected").html();
FabioDch
  • 516
  • 4
  • 8
0

Seems to me that rather then getting the actual html of the select box, you want to get the selected user, am I right? To do that, use

var userText = $("#my_SiteUsers option:selected").val();
  • 1
    Won't the val() function return the value and not the content? – Batman Sep 30 '13 at 19:51
  • According to this http://stackoverflow.com/questions/196684/jquery-get-specific-option-tag-text you can use .text() rather then .val() to get this –  Sep 30 '13 at 19:52