Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8998411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:01:22+00:00 2026-06-16T00:01:22+00:00

As shown in this example javascript-use-variable-as-object-name I am using eval to use a DOM

  • 0

As shown in this example

javascript-use-variable-as-object-name

I am using eval to use a DOM attribute to select an element from an array. Though there is no direct way for the user to change the input, I want to be as secure as possible and make sure that the variable is indeed an integer before I evaluated it.

Which of the following would be the best, most secure, way?

$(".listitem").click(function(){
    var id = $(this).attr("record-id");
    if(!isNaN(new Number(id))){
        Storage.search.nearby.currec = rowsHolder[eval(id)];
    }else{
        // send email to admin, shut down
    }
});

or

$(".listitem").click(function(){
    var id = $(this).attr("record-id");
    if(parseInt(id)){
        Storage.search.nearby.currec = rowsHolder[eval(id)];
    }else{
        // send email to admin, shut down
    }
});

More, but not required info:

Basically I am pulling down a large JSON string from online, containing an array of records. Upon building a table from the info using a for statement ( for(i in array) ), I push each row into an array called rowsHolder and give the tr an attribute of record-id=”i”. Then when the user clicks the row, I call the method you see above. I am using PhoneGap with JQuery Mobile.

As always, thanks for the input

-D

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T00:01:23+00:00Added an answer on June 16, 2026 at 12:01 am

    There is absolutely no reason to use eval here.

    • If your id is kind of a number, use parseFloat(id) to get it. Unnecessary as it would be converted back to a string when used as a property name, though.
    • If your id is an integer, use parseInt(id, 10) to get it. Unnecessary as it would be converted back to a string when used as a property name, though.
    • If your id is a string, just let it be a string. The property name you use it for would be one anyway.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In this example my NSDictionary initializes with 0 key/value pairs, as shown in my
So I'm having a bit of trouble understanding this example that was shown in
i don't understand this example from the doc : the timeIntervalSinceNow method should show
I am learning the Javascript tips from this http://code.google.com/p/jslibs/wiki/JavascriptTips site. I came to see
I would like to use a clock like this one: W3 schools Javascript Clock
I copied this example from api.jquery.com $('.target').change(function() { alert('Handler for .change() called.'); }); I
I have a situation in which I have to use eval in Javascript. Yes,
This example shows what I am trying to do: http://jsfiddle.net/Dqjvy/ I would like it
Currently, this example shows an unordered list with 8 list items in it. Is
I'm following this example: http://www.codinghorror.com/blog/2005/12/getting-started-with-indexing-service.html However, the conversion to dataset shows empty columns for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.