views:

10628

answers:

6

UPDATED - please read further details below original question

I have a select form element with various urls, that I want to open in a new window when selected - to do this I have the following code in the element's onchange event:

window.open(this.options[this.selectedIndex].value,'_blank');

This works fine. But I also want to submit the form when changing this select element value - I've tried various things, but I can't seem to get it to work.

I have jquery, so if it's easier to achieve via that then that's fine.

Update - I've just realised there is another issue with the above, because some of the urls are actually used to generate and output pdfs, and these do not work - they open and then immediately close (at least in IE7).

UPDATE 07/05/09 - I've now opened a bounty for this question as I really need to come up with a working solution. I did originally get around the issue by displaying links instead of a form select element, but this is no longer feasible.

The reason I need the above is that I have a large number of files that might need to be viewed / printed, too many to reasonably display as a list of links. I need to submit the form to record the fact a particular file has been viewed / printed, then display a log of the file history on the form - I'm comfortable with achieving this side of things, so don't require assistance there, but I thought it would help to place the context.

So, to clarify my requirements - I need a form select element and 'View' button that when clicked will not only launch a file download in a new window (note the above issue I faced when these files were PDFs), but also submit the form that contains the select element.

+1  A: 

You can use this.form.submit() to trigger the form submit:

<script language="javascript">
    function myChangeHandler() {
        window.open(this.options[this.selectedIndex].value, '_blank');
        this.form.submit();
}
</script>

<select onchange="myChangeHandler.apply(this)">
    ...
</select>
Aron Rotteveel
Hi Aron. I've tried this syntax but I get an 'object doesn't support this property or method' error. I thought the above would work too, but something seems to be throwing it off?
BrynJ
edited and tested the code now. this should work.
Aron Rotteveel
Very weird - I still get the same error? What could be causing this?
BrynJ
@BrynJ: There are two common errors that could cause that. Either there's an element in the form with the name or id "submit", and you'll have to change that in order to get to the form's "submit" method; or the object you have isn't of the form, and possibly is an array of forms or other elements with that id or name.
Anonymous
There was indeed an element named "submit" - thank for the advice, will try again!
BrynJ
A: 

You know you can set a target attribute for the form?

<form target="_blank" method="post">
  <select onchange="load()">
    ...
  </select>
</form>
<script>
  load() {
    document.forms[0].action = this.value;
    document.forms[0].submit();
  }
</script>

Forgive the probably bad Javascript. I tend to do more jQuery these days so I'm rusty on vanilla Javascript. But you get the general idea.

cletus
Sorry, I should have been clearer - the form's target isn't the new url, I simply want to submit the form after this specific select element is changed.
BrynJ
A: 

While I'm not familiar with jQuery, you should be able to do something like this (Prototype-style):

$('select-field').observe('change',function(event){
  window.open(this.options[this.selectedIndex].value,'_blank');
  this.form.submit();
}

Though I have had a few odd issues with submitting forms with Javascript before, so if that doesn't work you could try calling click() or the equivalent of fireEvent('click') on your form's submit button, like so:

$('submit-button').fireEvent('click');
Luke
Tried this but I get the same 'object doesn't support this property or method' error.
BrynJ
+3  A: 

Here ya go

<script type="text/javascript">
    $(function() {
        $("#selectElement").change(function() {
            if ($(this).val()) {
                window.open($(this).val(), '_blank');
                $("#formElement").submit();
            }
        });

        // just to be sure that it is submitting, remove this code
        $("#formElement").submit(function() {
            alert('submitting ... ');
        });
    });
</script>

<form id="formElement" method="get" action="#">
    <select id="selectElement">
        <option></option>
        <option value="http://www.deviantnation.com/"&gt;View Site 1</option>
        <option value="http://stackoverflow.com/"&gt;View Site 2</option>
        <option value="http://serverfault.com/"&gt;View Site 3</option>
    </select>
</form>
Chad Grant
+1 - There's no reason why this shouldn't work perfectly in every browser. If this doesn't work, then there's something wrong with BrynJ's other code somewhere.
KyleFarris
...and indeed it does :) And I've managed to resolve the issue I had with IE closing the pop up too.
BrynJ
+1  A: 

Just tested Aron's example and it works fine, so I would suggest the error you are getting is from code outside of your onchange event. Try the below working example and see if you get the same error.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <TITLE> Example onchange and submit </TITLE>

  <script language="javascript">    
   function myChangeHandler() 
   {
    window.open(this.options[this.selectedIndex].value, '_blank');
    this.form.submit();
   }
  </script>
 </HEAD>

 <BODY>
 <form id="myform1" action="test.html">
  <select onchange="myChangeHandler.apply(this)">
    <option>Please select....</option>
    <option value="http://stackoverflow.com"&gt;Stackoverflow&lt;/option&gt;
    <option value="http://twitter.com"&gt;Twitter&lt;/option&gt;
    </select>
  </form>
 </BODY>
</HTML>
Gais
+1  A: 

Basede on what you've described, your markup probably looks something like this:

<form ...>
  <input name="submit" ...>
  ...
</form>

Because browser tradition is to add to the form element's object (as properties) inputs' names, the "submit" property from the input masks the form's inherent "submit" property or method. You can correct this by renaming, even temporarily, the input element (assuming there's just the one):

form_object.elements['submit'].name = 'notsubmit';
form_object.submit();

If there are more than one -- eg, a series of radio buttons named "submit" for some reason -- then .elements['submit'] should be an element collection, which is like an array, which you can loop over to do the same thing.

Anonymous
Thanks for the useful explanation!
BrynJ