DCR
DCR

Reputation: 15700

Place focus on input select option

I have the following html:

<select id="selectBox"  name ="plus<?php echo $k; ?>">
        <option  value =" "></option>
        <option  value ="1">1</option>
        <option  value ="2">2</option>

and this script:

<script type="text/javascript">   
   $("#selectBox option:eq(1)").attr("selected","selected");
</script>

the above works but I need to pass a php variable for eq(1). I've tried the following but it doesn't work:

<script type="text/javascript">
   var x = <?php echo $_POST['cnt']; ?>;   
   $("#selectBox option:eq(x)").attr("selected","selected");
</script>

Upvotes: 1

Views: 584

Answers (2)

JungleZombie
JungleZombie

Reputation: 1181

You need to pass x as variable, not some string. Doing so

<script type="text/javascript">
   var x = <?php echo $_POST['cnt']; ?>;   
   $("#selectBox option:eq(" + x + ")").attr("selected","selected");
</script>

Upvotes: 1

DCR
DCR

Reputation: 15700

the following works:

<script type="text/javascript">   
$("#selectBox option:eq("+<?php echo $_POST['num_res'];?>+")").attr("selected","selected");
</script>>

Upvotes: 0

Related Questions