1

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>

4 Answers 4

4

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.

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

4 Comments

+1 for this. If this is the case, he can use $('input[value="Attach"]') as the selector.
@Jim Or, you know, he could just fix the id. ;)
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.
+2 one to your answer and comment, just to max out points lol
1

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]');

7 Comments

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.
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
I think it is because jquery is being mixed with regular javascript.
Define "didn't work"? What is it doing, and what are you expecting it to do?
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.
|
1

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!"

2 Comments

appreciate it Jim, It turned out to be a weird variable naming problem lol. + 1 for your time.
@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.
0

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

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.