0

I am developing a mobile application using phonegap and jquery mobile. I have this function which has to pass a variable to another function. It goes something like this:

    $.each(response.records, function(i, contact) {
       var url = contact.Id;
       var newLi = $("<li><a href='javascript:dothis("+url+")'>" + (i+1) + " - " + contact.Name + " - Company "+contact.Company+"</a></li>");
           ul.append(newLi);}

I have the dothis(argument) function but it does not get called when i put in the variable "url". When i erase the argument, it works. Please Help!

2 Answers 2

3

It's definitely not good practice to use the javascript: protocol in href attributes. It's much better to bind events to the links and respond accordingly.

Insert something like this after you append newLi to the ul:

$.find('a').bind('click', function() {
    dothis(url);
});

Here's some more info about why it's bad practice to use the javascript: protocol: Why is it bad practice to use links with the javascript: "protocol"?

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

1 Comment

thanks Philip Walton. I guess i was going for the 1990's thing :(
1

You need to put the url in quotes in the javascript:

var newLi = $("<li><a href=\"javascript:dothis('" +
      url +
      "')\">" + 
      (i+1) + " - " + contact.Name + 
      " - Company " + contact.Company + "</a></li>");

You might need to consider escaping the URL so that, if it contains any difficult characters, your javascript won't break.

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.