2

I need to associate a dynamic javascript function with checkbox along with other data in an array. For example. I have the following javascript array:

var arr = ['hello', 'world'];
arr.unshift('buddy');
var index = 1;
arr.unshift('<input type="checkbox" id = "' + index + '" onchange="boxChange(" + index + ")>');

The first element in the above array is a checkbox that has an onchange event. The event triggers the function boxChange that has the index passed in.

but the above checkbox event doesn't work, I guess the syntax (quotes) is not right. I need some help with correcting the "quotes". Thanks

I have an imcomplete code written in jsFiddle here http://jsfiddle.net/qq0dsz0f/

5
  • 1
    is there a reason for inline event handlers rather then in a unobtrusive style? Commented Jan 6, 2015 at 14:59
  • sorry, what's unobtrusive style? Commented Jan 6, 2015 at 15:00
  • it looks like you already know how to concatenate a string in js, so do the same for second index variable. But your quotes are mismatching anyway... Commented Jan 6, 2015 at 15:00
  • jsfiddle.net/qq0dsz0f/2 Commented Jan 6, 2015 at 15:01
  • Inspect the html itself in console, will see your quotes are breaking. Use proper escaping and matching. The syntax highlighting of code in your question should also be a clue Commented Jan 6, 2015 at 15:01

1 Answer 1

1

Generally speaking, mixing markup and logic is not a good idea. You can see for yourself -- as soon as you start wanting to manipulate the DOM in non-trivial ways, things get really hairy, really fast. You shouldn't have to worry about getting your quotes right, and you don't have to!

jQuery allows you to create an element on its own, then attach various event handlers to it and then store it, add it to your document, etc.

// First, let's create the element itself.
var element = $('<input type="checkbox" id="' + index + '">");

// Now, let's attach the logic.
element.on('change', function() {
    alert(index);
});

// Now do whatever you want with 'element'.

Isn't this syntax much cleaner?

Secondly, if you happen to use this inside a loop, you might get some strange behavior because of how the function captures the value of `index'. If this is the case you might want to see this question on capturing variables in JavaScript.

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

7 Comments

Actually it could get even cleaner if you used a delegate. Then you only need to attach the handler once: $('#table1').on('change', 'input[type="checkbox"]', function (e) { alert($(this).attr('id')); }
That's an even cleaner one. Neat!
Additionally you should mention he cannot use only a number for (ie. the index) for his id attribute... it needs to start with a letter... so something like checkbox-1 would be fine but just 1 is not.
@prodigitalson that's not true in html5 spec, numeric ID's are allowed
@charlietfl interesting... I did not know that. I haven't actually reviewed the spec in full just some of the newer features. Very good to know though for the record I think i would avoid it without a prefix it could get confusing with nothing else in the id.
|

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.