0

I start learning jquery and have the following code to getting the data from my database, it is corrected display what I want from the database in the 'div tag' below

 <script type="text/javascript">
            // function to get data from the database
            function getData()
            {
                $('#num').hide();
                $.post('getData.php',{rev: form.select.value},
                  function(output){
                          $('#num').html(output).fadeIn(700);        
                  });
            }
 </script>
 ....
 <div id="num"></div>
 .......

The jquery html method displays as a string (e.g: 1 2 3). Now I want to save the contents of jquery html method into an array so I can access it for future data usage, is there any way to establish that ? Thanks for your helps !!!

3
  • why are you trying to store that in a separate storage.? Any time you can access it through the .html() Commented Dec 7, 2013 at 8:02
  • could I be able to access only the first value of the contents ? e.g: 1. I want to use that value for something else Commented Dec 7, 2013 at 8:05
  • Can you show the contents? is it exactly 1 2 3 - separated by spaces? Commented Dec 7, 2013 at 8:14

3 Answers 3

1

Use output.split(' ')

<script type="text/javascript">
        var arr;
        // function to get data from the database
        function getData()
        {
            $('#num').hide();
            $.post('getData.php',{rev: form.select.value},
              function(output){
                      $('#num').html(output).fadeIn(700);
                      arr = output.split(' ');
              });
        }
</script>

You can access the first element using output.split(' ')[0]

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

Comments

0

You can use split or JSON.parse here:

Using JSON.parse:

var arr = JSON.parse("[" + output + "]");

output:

[1, 2, 3]

This gives you an Array of numbers.

or

output.split(" ");

Output:

["1", "2", "3"]

This gives you an Array of strings.

Comments

0

Ok, "it is corrected display what I want from the database in the 'div tag' below" so you don't want to split or re-format, you want to "save the contents of jquery html method into an array so I can access it for future data usage".

What you're asking is a little ambiguous still. Do you mean this? Save the data in a variable.

var somedata;

$.post('getData.php',{rev: form.select.value},
    function(output){
        $('#num').html(output).fadeIn(700);        
        somedata=output; // but then why not just use the var output?
    });
});

// somedata can be accessed at any time

Or this? The data is a CSV using space as the delimiter, so 1 2 3, save that as an array

var somedata;

$.post('getData.php',{rev: form.select.value},
    function(output){
        $('#num').html(output).fadeIn(700);        
        somedata=output.split(' ');
    });
});

// you can access somedata[0] which = 1, but only after the $.post has happened

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.