1

I m adding child divs of main div into array by id but couldn't get what is the problem.......? after adding into array i waant to send to ajax to write in csv

<!DOCTYPE html>
<html>
   <body>
        <p>Click the button to convert the array into a String.</p>
        <div id='main'>
            <div id='a'>
                dab
            </div>
            <div id='b'>
                nav
            </div>
        </div>

        <button onclick="myFunction()">Try it</button>

        <p id="demo"></p>

        <script>
            function myFunction() {
                var array = $('#main id').map(function() {
                    return $(this).val();
                }).get();
                array.toString();
                document.getElementById("demo").innerHTML = array;
            }
        </script>
   </body>
</html>
1
  • val() gets the value of an input, you have no elements with a value Commented Mar 2, 2016 at 20:09

1 Answer 1

2

Try to use attribute selector properly,

var array = $('#main [id]').map(function() {
   return $(this).text();
}).get();

Also .val() is a jquery function is specially for the elements which yields value property when accessing it on a node object. So when you want to access the content inside a div, you have to use .text()

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

1 Comment

@HamasMalik You have to configure your fiddle properly(add jquery library, wrap code in header tag). jsfiddle.net/L4yL7hjL/1

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.