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 3356594
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:31:54+00:00 2026-05-18T02:31:54+00:00

I am using jQuery to take data from an xml file and save it

  • 0

I am using jQuery to take data from an xml file and save it to a string. The string is a switch statement which I want to become a function.

$.get('type.xml', function(xml) {
var cases=[];
$(xml).find('type').each(function(){
    index=parseInt($(this).find('earn').text());
    if($.isArray(cases[index])){
        cases[index].push($(this).find('name').text());
    }else{
        arr=$.makeArray($(this).find('name').text());
        cases[index]=arr;
    }
});
var autoearn="function(x){switch(x){";
for(i=0;i<=100;i+=5){
    if($.isArray(cases[i])){
        $.each(cases[i], function(index,v){
            autoearn+='case "'+v+'":';
        });
        autoearn+='$("input[name^=\'earn\']").val('+i+');break;';
}}
autoearn+='}}';
});

The first section makes a 2D array that associates an array of cases to their resultant output by their index. So that something that earns $10 (e.g. Meeting) will be stored in cases[10] (Meeting=cases[10][0]).

The second part writes a string that makes a puts into a switch statement that looks something like this:

function(x){    
    switch(x){
        case "Meeting":
            $("input[name^='earn']").val(10);
            break;
        case "Event1":
        case "Event2":
            $("input[name^='earn']").val(20);
            break;
    }
}

Note that the syntax for the switch cases make Event1 and Event2 have the same result.

Now autoearn is just a string that has the code for the function I want. How do make into a function? Is there a better way to do this? I’m thinking of sending it through PHP to return it back to jQuery but this whole thing seems really indirect and there is probably an easier way to do this.

  • 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-05-18T02:31:54+00:00Added an answer on May 18, 2026 at 2:31 am

    First of all, make sure the code in the string is trusted, or you’ll run into problems by executing arbitrary code.

    You can create a function from a string as follows:

    var f = new Function('arg1', 'arg2', ..., 'function code');
    

    The above is equivalent to:

    function f(arg1, arg2, ...) {
      function code
    }
    

    In your case, you would want to remove the function(x) and the opening/closing braces from the autoearn string and create the function like this:

    new Function('x', autoearn)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Has anyone used jquery to take data from one form field and put it
Using jQuery, how would you find elements which have a particular style (eg: float:
When using jQuery 's ajax method to submit form data, what is the best
I am using jQuery autocomplete pluggin . I have the following code $().ready(function() {
I am using jquery ajax method in my app and from the web method
dear all, i'll take some data from the database. and join two tables.the code
I am attempting to pull some information from my tnsnames file using regex. I
Using jQuery , how can I dynamically set the size attribute of a select
Using jQuery, how do you bind a click event to a table cell (below,
Using jQuery, how do you check if there is an option selected in a

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.