Take the 2-minute tour ×

I have a select tag that looks something like this:

<form id="edit">
<select name="school">
<option val="1">School Name1</option>
<option val="2">School Name2</option>
<option val="3">School Name3</option>

the options are generated from php that are on a sql server, and the javascript is using the same data.

Now then, the jquery I'm using to select it by text is:

$('#edit select[name = school] option:contains('+name+')').attr("selected", "selected");

I tried some other variations too but none seem to work. So, where am I going wrong exactly? I'm using jquery 1.7.2 at the moment.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

Set selected to true:

.attr("selected", true);

Instead of:

.attr("selected", "selected");


share|improve this answer
Hrmm, it does indeed work on jfiddle. When I was researching this, it seemed to vary how to do this by jquery version. –  Samuraisoulification Apr 25 '13 at 2:25
Though it seems that the jsfiddle is set to 1.7.2 –  Samuraisoulification Apr 25 '13 at 2:26
This IS the jQuery version. –  Samuel Liew Apr 25 '13 at 2:29
Yeah, I noticed that as soon as I said it. What else could be wrong I wonder? –  Samuraisoulification Apr 25 '13 at 2:31

Your Answer


