4

I'm trying to pass a variable to a function, I know there are many topics about this, and I've nearly tried all suggestions, but still can't get it to work, these are my attempts:

edit: without the onclick, everything is working fine

var filename = file.name;

<button class="btn btn-danger delete" onclick="deleteImage(\''+filename+'\');">

results in: Uncaught SyntaxError: Unexpected token ILLEGAL

<button class="btn btn-danger delete" onclick="deleteImage(&quot;'+type+'&quot;);">

results in (alert): 'filename'

<button class="btn btn-danger delete" onclick="deleteImage('" + filename + "');">

results in: Uncaught SyntaxError: Unexpected token ILLEGAL

<button class="btn btn-danger delete" onclick="deleteImage(" + filename + ");">

result in: Uncaught SyntaxError: Unexpected token }

this is the full code (modified, blueimp fileuploader)

    <script id="template-download" type="text/x-tmpl">
{% for (var i=0, file; file=o.files[i]; i++) { %}
    <tr class="template-download fade">
        {% if (file.error) { %}
            <td></td>
            <td class="name"><span>{%=file.name%}</span></td>
            <td class="size"><span>{%=o.formatFileSize(file.size)%}</span></td>
            <td class="error" colspan="2"><span class="label label-important">Error</span> {%=file.error%}</td>
        {% } else { 
            // add the image urls to the file inputbox
            var filename = file.name;
            var prev = $("#mFile").val(); 
            $("#mFile").val(prev + file.name + ","); 

            %}
            <td class="preview">{% if (file.thumbnail_url) { %}
                <a href="modules/mod_stern_form_prijsopgave/upload/server/php/files/{%=file.name%}" title="{%=file.name%}" data-gallery="gallery" download="{%=file.name%}"><img src="modules/mod_stern_form_prijsopgave/upload/server/php/files/thumbnail/{%=file.name%}"></a>
            {% } %}</td>
            <td class="name">
                <a href="modules/mod_stern_form_prijsopgave/upload/server/php/files/{%=file.name%}" title="{%=file.name%}" data-gallery="{%=file.thumbnail_url&&'gallery'%}" download="{%=file.name%}">{%=file.name%}</a>
            </td>
            <td class="size"><span>{%=o.formatFileSize(file.size)%}</span></td>
            <td colspan="2"></td>
        {% } %}
        <td>
            <button class="btn btn-danger delete" onclick="deleteImage('" + filename + "');" data-type="{%=file.delete_type%}" data-url="{%=file.delete_url%}"{% if (file.delete_with_credentials) { %} data-xhr-fields='{"withCredentials":true}'{% } %}>
                <i class="icon-trash icon-white"></i>
                <span>Verwijderen</span>
            </button>
        </td>
    </tr>
{% } %}
</script>

and testing like this:

    function deleteImage(filename) {
      alert(filename);
    }

what am I doing wrong? Thanks for your advice

2
  • 1
    What system is this? Looks like it might be jQuery templates or knockoutjs? This question needs better tagging. Commented Apr 11, 2013 at 12:12
  • yeah, jquery, I'll add the tag. Commented Apr 11, 2013 at 12:12

5 Answers 5

3

try the template format for onclick value:

onclick="deleteImage('{%=filename%}');"

try file.name as well

onclick="deleteImage('{%=file.name%}');" 
Sign up to request clarification or add additional context in comments.

1 Comment

lol..I've tried something close to this.. this worked! thank you so much. this might bring a grin upon your face: I just spent 2 hours on this
3
<button id="deleteOnClick" class="btn btn-danger delete">...</button>

and in javascript:

document.getElementById("deleteOnClick").onclick = function(){deleteImage(filename);}

edit:

if you want to delete the file specified only by the original filename value:

var deleteByFile = (function (filename){
    return function(){deleteImage(filename);};
}(filename));
document.getElementById("deleteOnClick").onclick = deleteByFile

3 Comments

all suggestions resulted in: Uncaught TypeError: Cannot set property 'onclick' of null
did you add the id to the button? if not then you can use $(".btn-danger").click(deleteByFile); instead.
fixed it now using gp.'s answer, thanks a lot for your help mate
2

You can't access JS variables from your HTML. You must do something like this:

var filename = 'testing';
document.getElementById('testdiv')
    .setAttribute('onclick', "alert('" + filename + "')");
filename = 'more testing';

Here is a fiddle.

6 Comments

I've tried this one aswell, results in: Uncaught SyntaxError: Unexpected token }
@MaartenHartman Well then it's a problem somewhere else in the code, because my code most definitely does NOT have a }.
I know, and normally your suggestion should work, but without the onclick everything is working fine, so the code isn't flawed.
thanks for your help so far, last suggestion resulted in: Uncaught TypeError: Cannot set property 'onclick' of null
@Maarten Well, is your div's id "testdiv"?
|
0

try to use onclick="deleteImage(" + filename + ");"

Comments

-1

You can set a varaiable like var counter = 1;

Wherever it is required, you can update it like

     onclick="updateCount('+counter+')"

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.