0

I have the following HTML:

<select name="1028071" class="LC" id="1028071" style="width: 100%;">
    <option selected="selected" value="-1">[Please select a value]</option>
    <option value="10604534" data-uid="154b2706-505e-401c-a066-a37a1889e3be">[Other]</option>
    <option value="10604535" data-uid="25070b3b-8215-440b-b3cf-c6cf52f01efc">Reason 1</option>
    <option value="10604536" data-uid="fd3394ea-a7e8-45e8-8385-71dee98238ff">Reason 2</option>
</select>

I am trying to pre-select [Other]. Both value and the text are variables. The only fixed value across environments is the data-uid.

I tried the following but it is not working:

var other = $(formalRequestReasonFieldId).find("option").filter(function() {
    return $(this).data('uid') == '154b2706-505e-401c-a066-a37a1889e3be';
});

alert(other.text()); // returns blank text
other.attr('selected', true); // does not work, no errors
4
  • 1
    $('option[data-uid="154b2706-505e-401c-a066-a37a1889e3be"]') Commented Jan 11, 2016 at 11:21
  • 1
    What you have should work fine: jsfiddle.net/ffw5quqr. Have you checked the console to ensure there are no errors, and the value of the uid you're searching for is what you expect it to be? Commented Jan 11, 2016 at 11:24
  • It should work, unless you are changing the uid value using the data api Commented Jan 11, 2016 at 11:26
  • @downvoter: care to comment? Commented Jan 11, 2016 at 11:37

1 Answer 1

1

If formalRequestReasonFieldId is the Select tag ID :

var other = $('#'+formalRequestReasonFieldId).find('option[data-uid="154b2706-505e-401c-a066-a37a1889e3be"]');

//Other Text
var text = other.text();

//Select It
other.prop("selected", true);

JSFiddle: https://jsfiddle.net/6opqnxhy/

Sign up to request clarification or add additional context in comments.

1 Comment

The missing part is '#'.. Thanks. Just forgot to add it to the formalRequestReasonFieldId variable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.