tags:

views:

137

answers:

5

I have a very simple JavaScript function:

function insertPost()
{
    document.postsong.submit()

    parent.document.getElementById('postSongButton').disabled = true;

}

Both commands in it work but only the first one will fire. This is true when they switch places also. Only the first one will fire...

+1  A: 

Did you check the casing of the html element?

Binoj Antony
Yes but as I said both of the parts work just not together so it has something do with how they are interacting I believe.
ian
+2  A: 
document.postsong.submit()

Submits the form, takes focus away from the function, function ends there

parent.document.getElementById('postSongButton').disabled = true;

Disables the button, so perhaps it is that there is then nothing to submit the form.

Not too sure if disabling the form button would stop the event from bubbling, but I suspect that the nature of these two lines will lead you to separating them, and having the second one in another event handler.

Hope this points you in the right direction.

Mark Dickinson
I was thinking something like that... How do would I separate them as you suggest?
ian
The problem described by the OP doesn't happen in Firefox, but happens in IE. I am able to duplicate it. Either the button gets disabled and the form does not submit or the form submits and the button never gets disabled.
Jose Basilio
I am using Chrome so I will try in FireFox
ian
Well, the simplest way would be to have some form field "postButtonDisabled", so you can persist the disabled status. Before you submit (1st line), set the field to "true". Then you can have the second line in an onLoad function. There are probably cooler ways to do this but it would work :)
Mark Dickinson
I could also put the Submit button in a <div> and hide that <div> at the start of the function hmmm?
ian
It might work, but you do still have the enabled button somewhere on the form, so it could make things awkward. Depends on how complex the page will end up being. Sorry in my previous comment the "postButtonDisabled" field can be a hidden field, so you get to set it's text to true, then have an onLoad function that reads it's value into the disabled property of the postSongButton. Hope this has helped :)
Mark Dickinson
+1  A: 

Hi, on click of the button you are calling the funcion insertPost().so what you have to do first disabled the button and then submit the form.one think i didnt understand why are using parent of object.

function insertPost() {

parent.document.getElementById('postSongButton').disabled = true;
document.postsong.submit();

}

Abhimanyu
Yes this works in FireFox but in Chrome... If a form is disabled you cannot submit it...
ian
+2  A: 

EDIT: On further inspection, I found that the real source of the problem is the line:

document.postsong.submit()

Here are the results of my tests in different browsers. If the line previous to the submit() is "button.disable = true", and the button type="submit":

  • Firefox disables the button and submits the form.
  • Chrome disables the button, but does not submit.
  • IE does not disable the button, but it does submit the form.

This explains the behavior you have been experiencing. Having parent before getElementById does not hurt anything, but it is not necessary. Change the code in your insertPost() function to this:

function insertPost(){
 document.getElementById("postSongButton").disabled = true;
 document.forms["postSong"].submit();
}
Jose Basilio
That's a nice hack +1
ichiban
.disabled=true does work in IE. It works for me!
Chandan .
@Ian - did you read and try my edited post?
Jose Basilio
A: 

You are using parent.document.getElementById(...

Just check if you are referring to the button correctly. i.e. if the parent reference you are using is correct. i.e if the button is in same page as the form or in the parent.

And yes, first you have to disable the button and then trigger the submit action for the form. When you do it the other way, you might end up navigating away to a different page and the disabling line may never execute.

But, since you said, disabling doesn't work for you at all, I thought if you were using wrong reference. Did it give any javascript errors for you when you tried to disable it?

Chandan .