2

I've got multiple buttons. These buttons are created dynamically. Each button has a unique button text.

How can I get the button text in to an array?

<button type="button" class "utClass">uniqueText1</button>
<button type="button" class "utClass">uniqueText2</button>
<button type="button" class "utClass">uniqueText3</button>

var butTxArr = [];
$("#intoArr").click(function(){
    //how to get the button text into butTxArr
});
1

2 Answers 2

1

You can use .map() function along with .get():

$('.utClass').map(function(){
  return $(this).text();
}).get();//returns ['uniqueText1','uniqueText2','uniqueText3']
Sign up to request clarification or add additional context in comments.

2 Comments

Can I do butTxArr.push($(this).text();) to get all into butTxArr array?
you can concat returned above array to get them in butTxArr .
1

Try This DEMO:

var butTxArr = [];
$("#intoArr").click(function(){
    $('.utClass').each(function(){
       var obj = $(this).text();   // get the text of button
       butTxArr.push(obj);         //Add it to an array  
    })
});

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.