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.

Community
  • 1
  • 1

4 Answers4

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).

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Removing the line var botao; won't help. See here: http://jsfiddle.net/nuno/3fJ3r/8/ –  Feb 18 '14 at 14:45
  • @NunoNogueira I dont understand real issue. – Deepak Ingole Feb 18 '14 at 14:46
  • 1
    @NunoNogueira — The error there is `);` is missing from your call to `ready`. The code to assign to a global is fine. – Quentin Feb 18 '14 at 14:46
  • @Pilot the line alert("id é " + botao); results in an error –  Feb 18 '14 at 14:47
  • @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. – Quentin Feb 18 '14 at 14:48
  • @NunoNogueira I dont see any error..as Quentin master said you are just missing `);` – Deepak Ingole Feb 18 '14 at 14:48
2

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

$(document).ready(function () {
                 ^ unclosed
Thoronwen
  • 574
  • 1
  • 5
  • 13
  • 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" – Quentin Feb 18 '14 at 14:49
  • @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 Feb 18 '14 at 15:17
  • @Thoronwen thanks! The thing is I'm used to Aptana IDE (it does that for me). JSFiddle doesn't... :-) –  Feb 18 '14 at 15:18
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);
    });
});
tomasbasham
  • 1,695
  • 2
  • 18
  • 37
  • 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" – Quentin Feb 18 '14 at 14:47
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/

Teoman shipahi
  • 47,454
  • 15
  • 134
  • 158