1

How can I assign the id of a submit button to a variable using JQuery?

I have the following HTML:

<form id="myForm">
    <input id="oknovo" type="submit" value="OK & Novo" />
    <input id="okfechar" type="submit" value="OK & Fechar" />
</form>

JS:

var botao;
$(document).ready(function () {

    $("#myForm input[type=submit]").click(function (event) {
        botao = $(this).attr('id');
        alert("id é " + botao);
    });
});

You may see the live JSFiddle here

I've already managed to indentify the clicked button wiht the help of this question but the problem now is to assign it to a global variable.

6
  • So what is the problem? Commented Feb 18, 2014 at 14:42
  • Why not using submit method? Commented Feb 18, 2014 at 14:42
  • 2
    @RajeshDhiman — Because then you couldn't tell which button was clicked. Commented Feb 18, 2014 at 14:43
  • @Quentin sir thats perfect catch/ Commented Feb 18, 2014 at 14:44
  • what about using "window.botao"? Commented Feb 18, 2014 at 14:45

4 Answers 4

2

Your code is correct, you just need to add the closing parenthesis ) at the end.

$(document).ready(function () {
                 ^ unclosed
Sign up to request clarification or add additional context in comments.

3 Comments

I was assuming that was a copy/paste error when forming the question, since the text does say "I've already managed to indentify the clicked button"
@NunoNogueira If this happens more often, it might be a good idea that whenever you need to type (, you type ) immediately and then move the caret in between. That way, you can never forget any :)
@Thoronwen thanks! The thing is I'm used to Aptana IDE (it does that for me). JSFiddle doesn't... :-)
2

the problem now is to assign it to a global variable.

Either remove the line var botao; or take it out of the function you are calling onload (via the JSFiddle configuration on the left).

6 Comments

Removing the line var botao; won't help. See here: jsfiddle.net/nuno/3fJ3r/8
@NunoNogueira I dont understand real issue.
@NunoNogueira — The error there is ); is missing from your call to ready. The code to assign to a global is fine.
@Pilot the line alert("id é " + botao); results in an error
@NunoNogueira — No, it doesn't (the only error is described in my last comment, and I was assuming that was a copy/paste problem when forming the question since you said you were successfully getting the value) … and that comes after you assign botao to a global (which is what you claim the problem is) anyway.
|
1

You are missing brackets on your fiddle. It should read:

var botao;
$(document).ready(function () {

    $("#myForm input[type=submit]").click(function (event) {
        botao = $(this).attr('id');
        alert("id é " + botao);
    });
});

1 Comment

I was assuming that was a copy/paste error when forming the question, since the text does say "I've already managed to indentify the clicked button"
0

This question does not makes any sense, since code looks like working except syntax errors; on the other hand, you can use hidden fields to keep some data for temporary.

<form id="myForm">
    <input id='myGlobalHidden' type='hidden'/>
    <input id="oknovo" type="button" value="OK & Novo" />
    <input id="okfechar" type="button" value="OK & Fechar" />
</form>


var botao;
$(document).ready(function () {

    $("#myForm input[type=button]").click(function (event) {
                botao = $(this).attr('id');
        //alert("id é " + botao);
        $("#myGlobalHidden").val(botao);
        // access here
        alert($("#myGlobalHidden").val());
    });
});

http://jsfiddle.net/3fJ3r/10/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.