0

I have in Javascript:

 for ( i=0; i < parseInt(ids); i++){
   var vst = '#'+String(img_arr[i]);
   var dst = '#'+String(div_arr[i]);
  }

How can I continue in jQuery like:

    $(function() {
     $(vst).'click': function() {
      ....
     }
    }
2
  • $(vst).click(function() { }); Commented Apr 2, 2012 at 9:22
  • 1
    $(vst).'click': function() { is just completely wrong syntax. I recommend to make yourself familiar with the JavaScript syntax first: developer.mozilla.org/en/JavaScript/Guide and reading a jQuery tutorial probably helps as well: docs.jquery.com/Tutorials Commented Apr 2, 2012 at 9:39

5 Answers 5

1

NO, like this instead

 $(function() {
     $(vst).click(function() {
         ....
     });
 });

There are other ways depending on your version of jquery library

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

1 Comment

Thanks a lot! Will actually wait 5 minutes to accept the answer although it's about 5 AM already )
0

regarding to this, your vst must need to be an object which allow you to click on it, and you assign a class or id to the object in order to trigger the function and runs the for...loop

correct me if I am wrong, cause this is what I get from your question.

Comments

0
$(function() {
 $(vst).click(function() {
  ....
 }
})

You can use any string as element selector param for jQuery.

Read the docs for more information.

http://api.jquery.com/click/

http://api.jquery.com/

Comments

0

You can pass a String in a variable to the $() just the way you want to do it.

For example you can do:

var id = 'banner';
var sel = '#'+id;
$(sel).doSomething(); //will select '#banner'

What's wrong is the syntax you are using when binding the click handler. This would usually work like:

$(sel).click(function(){
//here goes what you want to do in the handler
});

See the docs for .click()

4 Comments

Or you could be a little briefer - $("#" + id).click(function() {})
@RemarkLima I'm just trying to "get the message across", won't be winning any performance awards though....
of course :) Hence the upvote! Just trying to keep the good content on SO, with more detail for those that need it
Accidentally downvoted, apparently, and now I can't undo it (it says it's locked). Sorry!!!
0

Your syntax is wrong, but other than that you will have no problem with that. To specify a click:

 $(function() {
      for ( i=0; i < parseInt(ids); i++){
          var vst = '#'+String(img_arr[i]);
          var dst = '#'+String(div_arr[i]);

          $(vst).click(function (evt) {
                ...
          });
      }
 })

Note that since vst is changing in the loop, your event code should also be placed in the loop.

EDIT: Assuming you want the same thing to happen for each image and each div, you could also do something like this:

$(function () {
    function imgEventSpec($evt) {
        // image clicked.
    }

    function divEventSpec($evt) {
        // div clicked.
    }

    for (var idx = 0; idx < img_arr.length && idx < div_arr.length; idx ++) {
        $("#" + img_arr[idx]).click(imgEventSpec);
        $("#" + div_arr[idx]).click(divEventSpec);
    }
});

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.