Skip to content

Instantly share code, notes, and snippets.

@webmechanicx
Last active May 3, 2017 19:04
Show Gist options
  • Save webmechanicx/35c7d46f55df51e1db315079129ea9fd to your computer and use it in GitHub Desktop.
Save webmechanicx/35c7d46f55df51e1db315079129ea9fd to your computer and use it in GitHub Desktop.
Select an option by its text, If you need to check if a selectbox has an option whose TEXT is a specific value. Example JIT site
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Textbox Value to Switch Selectbox</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
</head>
<body>
<input id="textvalue" type="text" value="Boy">
<select id="mySelect">
<option value="x">A</option>
<option value="xx">Boy</option>
<option value="xxz">B</option>
<option value="xxx">C</option>
</select>
<input type="button" id="my_button" value="test me"/>
<script>
$(function(){
$('#my_button').click(function(){
var value = $('#textvalue').val();
$('#mySelect option:contains(' + value + ')').each(function() {
if ($(this).text() == value) {
$(this).attr('selected', 'selected');
return false;
}
else {
$(this).removeAttr('selected');
}
return true;
});
});
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment