1

I'm trying to pass JSON array values into my javascript file and use the values as selectors to call methods on corresponding html divs.

for example:

function clickEvents(items) {
for (var r = 0; r < items.length; r++) {    
    var omega = items[r].id +;

     $(omega).click(function() {
        alert('Thanks for adding item number #' + omega ' to your cart!');
    });
}

where my JSON looks like this:

 {
"Items" : [{
        "header: "apple",
        "id": 5000
      }, {
         "header": "banana",
         "id":5001
      }, {
         "header": "pear",
         "id": 5002
      }]
 }

unfortunately, its not working properly. I can't seem to get the selectors to match up. Does anyone know the proper syntax for this type of approach or have ideas for other approaches?

when i do this, it works:

function clickEvents(items) {
   $('#5001').click(function() {
      alert('hi');
   });
 }

3 Answers 3

2

Two things.

First, you can't start an ID with a number. http://www.w3.org/TR/html4/types.html#type-id

Second, you need to concatenate the # in the selector if you are selecting using ID attribute.

Here's an example: http://jsfiddle.net/3BBtU/

function clickEvents(items) {
    for (var r = 0; r < items.length; r++) { 

          // removed the stray "+" at the end
        var omega = items[r].id;

            // If you wanted to add an attribute to each element, do it here.
        $('#' + omega).attr('someAttribute', 'some value');

            // Wrap handler assignment in a closure, passing "omega" in so that
            //      when it fires, it is referencing the correct value of "omega"
        (function(o) {
                 // concatenate "#" to the ID for a proper ID selector
             $('#' + o).click(function() {

                  // You were missing the second + operator in this concatenation
                alert('Thanks for adding item number #' + o + ' to your cart!');
             });
        })(omega);
    }
}

    // Modify IDs so that they do not start with a number
var obj = {
    "Items" : [{
            "header": "apple",
            "id": "d5000"
          }, {
             "header": "banana",
             "id": "d5001"
          }, {
             "header": "pear",
             "id": "d5002"
          }]
     };

    // call the clickEvents() passing in the Items from the obj variable
clickEvents(obj.Items);​

EDIT:

  • Added an example
  • Corrected a mistake where I had functions instead of var.
  • Corrected one more issue. I wrapped the handler assignment in a closure because the omega variable was being used inside the handler in the concatenation in .alert(), which means that it was referencing the latest value update of omega when a click fired it.
Sign up to request clarification or add additional context in comments.

6 Comments

so the id needs to be of type string? right now they are integers
A question about the first thing: start an ID with a number works; is it NOT standard?
@fordays, @s.susini - Whether it is a string or an integer, it is invalid HTML to start an ID with a number. They must start with a letter. It may work in some browsers, but perhaps not in others.
what about creating your own in-house attribute tag, for example: "pid": "k5001" as an element of an array. how do I refer to "pid" in a jquery selector after assignment the pid number to the html tag?
@fordays - Why? If you're going to start it with "k", then it is valid for an ID. You could use your approach, but it is faster to use actual valid ID attributes for lookup. A lot faster. Note that I'm talking about the ID for the HTML elements. Those must start with a letter.
|
0

What exactly is going wrong? You're missing a quotation mark after the last "id" in your sample, but maybe it's just a typo...(?)

1 Comment

i fixed the typo, however, I need help getting the selector syntax correct when I call $(omega).click Do I need to make a change in the assignment of omega or in the selector syntax?
0

You have to use something like: $("#"+omega). You are asking for an id, isn't it!?

function clickEvents(items) {
for (var r = 0; r < items.length; r++) {    
    var omega = items[r].id;

     $("#"+omega).click(function() {
        alert('Thanks for adding item number #' + omega ' to your cart!');
    });
}

In fact th $() function in this case thake a string the is a validCSS selector.

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.