views:

55

answers:

4

Hey guys, quick question, I have this javascript/jquery function that basically wraps an input in image brackets, and the first part of the function works, but the button does not disable after and I cannot figure out why (last line does not work). If anyone has an idea, let me know. Thanks in advance.

<input id="2image" type="button" value="Attach" onclick="imageid('message')">

<script>
    function imageid(input) {
        var obj=document.getElementById(input);
        obj.value+="[image]image[/image]";

        $("#2image").attr({ disabled:true, value:"Inserted" });
    }
</script>
A: 

Try setting $("elements").attr('disabled', 'disabled');

Chris Farmer
did not work, thanks for the suggestion though.
Scarface
+1  A: 

You need to set the disabled attribute to "disabled", not to true, and I personally prefer using .val() to set values.

$("#2image").attr('disabled', 'disabled').val('Inserted');

A couple of observations:

The last line is jQuery, while the preceding lines are vanilla JavaScript. Are you sure you're including the jQuery in your page via a <script type="text/javascript" src="jquery-1.4.2.js"></script> tag?

If you are indeed including jQuery, you should consider replacing your getElementById call with the jQuery operator ($('#' + input), or just $(input) if you modify to calling code to prepend the #):

$(input).val($(input).val() + '[image]image[/image]');
meagar
I am positive jquery is included, but it is 1.3.2. For some reason your suggestion did not work. Is it because there is normal javascript proceeding it? The first part attaches text to a textarea. I suppose that could easily be accomplished in Jquery, I just left it because I did it a long time ago and it worked.
Scarface
I tried changing var obj=document.getElementById(input);obj.value+="[image]image[/image]";to$(input).val($(input).val() + '[image]image[/image]');and the top part stopped working
Scarface
I think it is because jquery is being mixed with regular javascript.
Scarface
Define "didn't work"? What is it doing, and what are you expecting it to do?
meagar
well by didn't work I mean nothing happened after I tried your suggestion and switched the code, like outline above. I click the button and I get nothing. I expect it to a.) append text (image]image[/image] for example) to a textarea: #message, which the javascript somehow accomplishes and the jquery does not and b.) To disable the button after clicked
Scarface
Try commenting out the first two lines, leaving the `$('#2image')...` line intact - that much works. If the button still doesn't disable itself when clicked, the problem is external to the code you posted.
meagar
Thanks meagar, it turned out to be a really weird problem. I appreciate your time though, and I will change this javascript to jquery where I am more comfortable. +3
Scarface
+4  A: 

You may just be stumbling about this trivial limitation:

http://www.w3.org/TR/html401/types.html#type-name

ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").

I.e. id="2image" is invalid.

deceze
Note that I think most browsers will just glance over this, but who knows...
deceze
+1 for this. If this is the case, he can use `$('input[value="Attach"]')` as the selector.
Jim Schubert
@Jim Or, you know, he could just fix the id. ;)
deceze
That was an extremely key observation lol. You were correct. 2image was actually 2$image and a variable in php I just changed it back to make it look more simple, glad I left 2 in so you saw that, but the variable used hyphens and periods because it was an image name. Thanks a lot that was a really weird problem lol. I am so glad you pointed that out. It would have taken me hours. It is good to know as well for future reference in variable naming.
Scarface
+2 one to your answer and comment, just to max out points lol
Scarface
+1  A: 

Just access the DOMElement and change it via vanilla javascript:

$("#2image").val("Inserted").get(0).disabled = true;

$('input[value="Inserted"]').get(0).disabled = true;

I just ran this in the Javascript Console in Google Chrome to change the Add Comment button to disable and display "Gotcha!"

Jim Schubert
appreciate it Jim, It turned out to be a weird variable naming problem lol. + 1 for your time.
Scarface
@Scarface: Thanks. I had the wrong code pasted anyway. I've updated my answer in case anyone finds this question and it's not the naming convention.
Jim Schubert