2

I want to get a button ID onclick and apply it to another function. This is the code I have :

<button id="image-upload-button" onClick="reply_click(this.id)" data-link="<?php echo base_url().'admin/phones/upload_image/'; ?>" input-name="image" on-change="imageUpload(this.form);"></button>

and Javascript :

    //get button id
    function reply_click(clicked_id)
    {
        var button_id = clicked_id;
    }

    function reset() {
        var input_name = $('#' + button_id).attr('input-name');
        var on_change = $('#' + button_id).attr('on-change'); 
        var input = $('<input name="'+ input_name +'" type="file" onchange="'+ on_change +'"/>').appendTo(form);
        input.change(function(e) {
            input.unbind();
            input.detach();
            btn.trigger('choose', [input]);
            reset();
        });
    };

but, when I add $('#' + button_id) whole the script stops working. how can I fix it

1
  • 1
    You declared button_id in the function scope. So it only exists within the reply_click function. I think you meant to declare it in the global scope, outside of all functions. Commented May 2, 2012 at 20:18

2 Answers 2

3

you have declared button_id inside the scope of the reply_click() function. You should declare it outside, as a global variable, and just assign it in the reset_click() function.

Sign up to request clarification or add additional context in comments.

Comments

2

button_id must be visible to reset. Declare it outside:

var button_id;

//get button id
function reply_click(clicked_id)
{
    button_id = clicked_id;
}

2 Comments

but how can I do that?! I mean making it visible to reset function ?!
@Afshin declare var button_id outside of the function and remove var from var button_id inside reply_click, as noted above..

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.