1

I am trying to pass a string into a function using the onclick Event. However when I click the link the variable inside the function returns nothing. It is empty. I have no issues when I pass a number as an argument and that is displayed in the alert with no issues. I am not sure what I am doing wrong.

for (var j=0;j<current_order.length;j++){   
    var note_id = "note" + (j+1);
    text = text + "<button onClick= 'deleteItemfromOrder("+j+")'>Delete</button>";  
    text = text + "<a id = '"+note_id+"' onClick= 'toggleNote("+note_id+")'>Notes</a>";
    alert(text);
}

This is the function I am trying to pass note_id to:

function toggleNote(showHideDiv) {
    alert(showHideDiv);
}

I have tried multiple methods to pass the string as an argument to the function EG:

text = text + "<a id = '"+note_id+"' onClick= 'toggleNote('"+note_id+"')'>Notes</a>";

and

text = text + "<a id = '"+note_id+"' onClick= 'toggleNote(\'"+note_id+"\;)'>Notes</a>";

However every time the string appears empty on the other side in the function. There is no issues if I pass a number eg:

text = text + "<a id = '"+note_id+"' onClick= 'toggleNote("+2+")'>Notes</a>";
1
  • Also can someone say why this has been downvoted? Commented Nov 15, 2016 at 23:36

1 Answer 1

1

Your problem is that you're missing quotes, numbers don't need quotes, as in

onclick='toggleNote(3)'

but a string does

onclick='toggleNote("string")'

Also, using the same quotes won't work either

onclick='toggleNote('string')'

meaning you have to do

text = text + "<a id = '"+note_id+"' onClick= 'toggleNote(\""+note_id+"\")'>Notes</a>";

But really what you should do, is create elements

for (var j = 0; j < current_order.length; j++) {
    (function(i) {
        var btn = document.createElement('button');
        var anc = document.createElement('a');

        btn.addEventListener('click', function() {
            deleteItemfromOrder(i);
        });

        btn.textContent = 'Delete';

        anc.addEventListener('click', function() {
            toggleNote("note" + (i+1))
        });

        anc.textContent = 'Notes';
    })(j);
}
Sign up to request clarification or add additional context in comments.

3 Comments

or just reference the id and do not worry about passing the string.
That works great. Everywhere I looked however they use single quotes and speech mark like double quotes for strings in javascript. How do I know when and where to use the different ones and when to use escaping I think its called ( these \ backslashes).
@FKhan - if you have an attribute, and that attribute uses single quotes, anything inside it must use double quotes, otherwise the attribute is closed when you add a single quote, and since your string is defined with doublequotes in javascript, they must be escaped to not close the string literal.

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.