views:

8439

answers:

5

Hi

I'm trying to submit a form with javascript. Firefox works fine but IE complains that "Object doesn't support this property or method" on the submit line of this function:

function submitPGV(formName, action)
{
    var gvString = "";

    pgVisibilities.each(function(pair) {
        gvString += pair.key + ":" + pair.value + ",";
    });

    $('pgv_input').value = gvString;

    var form = $(formName);
    form.action = action;
    form.submit();
}

Called here:

<a href="javascript:submitPGV('ProductGroupVisibility','config/productgroupvis/save')">

Here's the form:

<form id="ProductGroupVisibility" action="save" method="post">
    <input type="hidden" name="ows_gv..PGV" id="pgv_input" value=""/>
</form>

Any ideas?

A: 

Are you sure you have your JavaScript library loaded? (jQuery or Prototype)

It worked for me in IE7 with Prototype.

Try:

alert($('ProductGroupVisibility').id)

See if you get an error.

Diodeus
You're right actually, it works for me if I put it into a dummy page with only the code listed above. It must be something else about the page. I'll amend my question with more info. Thanks for your help.
Kenny
+2  A: 

Try checking the type of the element IE is selecting:

alert( typeof( $( 'ProductGroupVisibility' )));

It is possible there is something else on the page with that ID that IE selects before the form.

Brian
Yep. I'm a moron. There was a table with id="productGroupVisibility", Firefox got the form, IE got the table.I was sure I'd already tried changed the id on that table but I wasn't the one at the keyboard so who knows what happened.Thanks heaps mate.
Kenny
No problem. Done it myself more than once ;)
Brian
Just want to add something that I've just seen that does the same thing. Different case, but same symptoms. Might help someone who stumbles on this answer."email = $('#UserEmail');"No duplicate id, but IE didn't like that email wasn't officially declared. Changing to:"var email = $('#UserEmail');"Fixes the problem. This is more correct and I guess that's what happens when you get used to programming in more permissive environments. :)
Erik Nedwidek
+1  A: 

What javascript framework are you using? If it's jQuery I think you'll need to add # to your id:

$('#ProductGroupVisibility').submit();
Lance McNearney
Depends on the framework, and selected plugins. Out-of-the-box mootools selects (at least v1.11 did) based on id with the syntax Kenny described.
Brian
Yah, I realized that after posting and edited my answer to specify jQuery. :)
Lance McNearney
Oh it's Prototype. Sorry I forgot to mention.
Kenny
+4  A: 

What name does your <input type="submit"> have?

If you called it "submit", you have overridden the form.submit() function, much the same way an input called "foo" would generate a form.foo property. That would explain the behavior.

Tomalak
I don't have an input of type submit. It's just the one hidden field that's submitted with javascript.
Kenny
Good grace, I just tripped over exactly this problem, Tomalak. I owe you a beer ;-)
Simon
Same here. This is one of the little gems of SO. :)
HabarNam
Wow, that's two beers already. :-)
Tomalak
+1 for me as well, this screwed me up!
Wil
A: 

If you called it "submit", you have overridden the form.submit() function, much the same way an input called "foo" would generate a form.foo property. That would explain the behavior.

Thank you!!! :))) I spent few hours digging it :)))

DonSleza4e