0

I want every dropdown option to have a target of "_blank" except option 4. How do I switch the target based on the option selected in the form?

    <form action="mypage.php" method="post" target="_blank" />
<input type="hidden" name="turnaround" value="20">
<table>
    <tr>
        <td>Platform</td>
        <td>
            <select required id="cu" name="plat" style="min-width: 120px;">
                <option value="1">1</option>
                <option value="2">2 (Desktop Only)</option>
                <option value="3">3 (Mobile Only)</option>
                <option value="4">4 (Wildcard)</option>
                <option value="5">Catch-All</option>        
            </select>
        </td>
    </tr>
    <tr>
        <td colspan="2">&nbsp;</td>
    </tr>
</table>
<div><input type="submit" value="Select Platform" /></div>
</form>
  • Is there any reason you can't hardcode them? Also, [this post](http://stackoverflow.com/questions/2000656/using-href-links-inside-option-tag) may help. – jmargolisvt Jun 07 '15 at 14:50
  • 1
    Also there is typo in your code, there is `/` at the end of starting form tag. – vinayakj Jun 07 '15 at 14:58

3 Answers3

0

give your form an id:

<form action="mypage.php" id="myForm" method="post" target="_blank" />
<input type="hidden" name="turnaround" value="20">
<table>
    <tr>
        <td>Platform</td>
        <td>
            <select required id="cu" name="plat" style="min-width: 120px;">
                <option value="1">1</option>
                <option value="2">2 (Desktop Only)</option>
                <option value="3">3 (Mobile Only)</option>
                <option value="4">4 (Wildcard)</option>
                <option value="5">Catch-All</option>        
            </select>
        </td>
    </tr>
    <tr>
        <td colspan="2">&nbsp;</td>
    </tr>
</table>

your js:

$('#myForm').attr("target") = $('#cu').val() == "4" ? "" : "_blank";

not tested but it should work

messerbill
  • 5,499
  • 1
  • 27
  • 38
0

May be you want to give class or id to submit button.

$('input[type=submit]').click(function(e){
    if($('#cu').val() == '4'){
        $(this).closest('form').prop('target','');    
    }  
});
vinayakj
  • 5,591
  • 3
  • 28
  • 48
0

Check this fiddle

js:

$("#cu").on("change",function(){
    var a = $(this).val();
    if(a == "4"){
         $("#formId").removeAttr("target")
    }else{
    $("#formId").attr("target","_blank")
    }

})
Ajay Narain Mathur
  • 5,326
  • 2
  • 20
  • 32