0

I have a button that is appended on a form. The form is dynamically created at some point in the application, and the button is appended like so:

$('#imgform').append("<center><input type='submit' value='Upload' id='#imgupload' onclick='showUploadedItem()'/>");

This is my code for showUploadedItem():

function showUploadedItem () {
    $('#upload_process').show();
            // a bunch of other things to do
           $('#imgupload').prop('disabled',true);
           console.log("the submit button ID is: " + $('#imgupload').attr('id'));
}

But the console.log message says the button ID is undefined, which means that at this point in the code the button doesn't even exist. I can understand that it was dynamically created and perhaps I need to bind the event, but how did it even get clicked then, and even show the console message at all?

How can I disable this button within the function? Or is it not even possible?

3
  • 3
    maybe because you have an # in the html of the id of the button ;p Commented Jan 28, 2013 at 8:35
  • 1
    oh my... that was silly. sigh! i spent a while on this! Commented Jan 28, 2013 at 8:59
  • happens !_! , all it takes is extra couple of eyes Commented Jan 28, 2013 at 9:01

4 Answers 4

3

Your html is using id="#imgupload" which does not appear legal. Although for fun I was able to get a selector this to work:

 $('#\\#imgupload').prop('disabled',true);

That sure looks like a mistake though and your html should simply be id="imgupload"

fiddle

Also if you're using jQuery why use the inline javscript? Simply bind an event handler (see fiddle for that too).

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

Comments

2

It is because you habve an error in the HTML, better, in the way you note the ID. The # is used to reference an ID-Name in CSS or in jQuery (#+Name). In HTML it is only the name, like this:

$('#imgform').append("<center><input type='submit' value='Upload' id='imgupload' onclick='showUploadedItem()'/>");

Comments

2

You dont need to give # before id. Remove that from the code

Change like this,

$('#imgform').append("<center><input type='submit' value='Upload' id='imgupload' onclick='showUploadedItem()'/>");

Comments

1

Remove # from your button id will fix that.

Comments

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.