3

$("#keyword-content").html()

Produces

  <p>
    javascript
  </p>
  <p>
    ruby
  </p>
  <p>
    python
  </p>

How can I convert this to ["javascript", "ruby", python"] ?

2 Answers 2

6

You can use the map method:

var arr = $('#keyword-content p').map(function(){
      return $(this).text()
}).get()
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @undefined, this is a very clean pattern. Waiting to accept
0

Something like this:

var myArray = [];

$("#keyword-content p").each(function(index,item) {
    myArray.push($(item).text());
});

1 Comment

Thanks @Matt but this throws an error #<HTMLParagraphElement> has no method 'text'. I tried something similar with underscore.js before I came to SO for help.

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.