0

Is there a jQuery selector to replace :contains with a more definitive indexOf, for example? I am trying to match for variable VALUE_XYZ

The problem is that the code finds strings that are similar (because it contains the string) before it gets to the value that is exact.

var value= $("select[name=BU_CT_NUM_DOC]").find('option:contains($VALUE_XYZ)').val();

The entire code looks like this and is encapsulated by xslt:

<xsl:when test="/Root/Form/EDIT_BU_TYPE = 'd' or /Root/Form/EDIT_BU_NOTIFICATION_TYPE='d'"><![CDATA[
 var value = $("select[name=BU_CT_NUM_DOC]").find('option:contains($VALUE_XYZ)').val();
 $("#BU_CT_NUM_DOC").val(value).attr('selected',true).trigger("chosen:updated");
]]></xsl:when>

Thanks!

1 Answers1

0

This is not an index of selector, but using the filter technique from How to select an option by its text? you can match the entire text of the string, or you could modify the filter function to use indexOf.

var $VALUE_XYZ = "AB";
var value = $("select[name=BU_CT_NUM_DOC]").find('option').filter( function() { return $(this).html() == $VALUE_XYZ } ).first().val();
console.log( value );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="BU_CT_NUM_DOC">
<option value="abc">ABC</option>
<option value="ab">AB</option>
</select>
JasonB
  • 6,243
  • 2
  • 17
  • 27