Nick Foote
Nick Foote

Reputation: 2505

How to get option text from a dijit.form.Select?

I have a dijit.form.Select on my page:

<c:set var="qualId" value="${previous.qualification.id}" />
<select id="qualification" name="qualification" dojoType="dijit.form.Select" onchange="checkForPHD()">
    <option value="-1" label="                      "> </option>
    <c:forEach items="${requestScope.qualifications}" var="qualItem">
        <c:choose>
        <c:when test="${qualId eq qualItem.id}">
            <option value="${qualItem.id}" selected = "selected">${qualItem.name}</option>
        </c:when>
        <c:otherwise>
            <option value="${qualItem.id}">${qualItem.name}</option>
        </c:otherwise>
        </c:choose>
    </c:forEach>
</select>

Then some javascript that I'm trying to use to set some text to the TEXT of the option chosen from the select box;

    function checkForPHD() {    
        dojo.byId('clazzPHDMessage').innerHTML = dojo.byId('qualification')
           .attr('displayedValue');
    }

I'd read that the .attr('displayedValue') was suppose to get the text from the selected option in a dijit.form.Select but it doesn't seem to do much? .attr('value') got that values ok but I need the TEXT?

Upvotes: 4

Views: 18674

Answers (4)

Lova Chittumuri
Lova Chittumuri

Reputation: 3323

You can try the below Code Snip

dijit.registry.byId("regionList").value;

<select name="regionList" id="regionList" data-dojo-id="regionList" data-dojo-type="dijit/form/Select">
<option value="" selected="true">Select LoB</option>
<option value="Asia" >Asia</option>
<option value="Africa" >Africa</option>
</select>

Upvotes: 0

Adalberto Costa
Adalberto Costa

Reputation: 1

You should try to get the selected node first, then get the attribute you want, as follows:

dijit.byId("qualification").getSelected().attr('innerHTML');

Upvotes: 0

Andrei
Andrei

Reputation: 4237

You should use dijit.byId() to get widget instance. Try to use this code to get selected text:

dijit.byId('qualification').attr('displayedValue')

Upvotes: 15

Michael Berkowski
Michael Berkowski

Reputation: 270767

Seems like what you want is the innerHTML of the currently selected option (<option>THIS TEXT??</option>). I think this should do the trick. Loop over all the select's options with getOptions and find the selected one, then return its innerHTML. I don't know if dijit.form.Select has a selectedIndex property, but that would help too.

function getSelectedText() {
  dojo.foreach(dijit.byId("qualification").getOptions(), function(opt, i) {
    if (opt.selected) {
      return opt.innerHTML;
    }
  });
}

Upvotes: 0

Related Questions