0

I have a list of buttons that is created by the DOM which references an array. When a button in the list is clicked, I want to retrieve the String that is displayed on the Button.

I have tried the following code to reference the string value, but get undefined: this.String; inside the function when the button is clicked to retreive the string.

How can I properly retrieve the string.

The click handling function is:

$('.timeButtons').click(function() {
    confirmation.push(this.textContent);
})

This is how the list of buttons is created:

var populateList=function(array){
    var list = document.createElement('ul');
    list.className="delete";
    for(var i = 0; i < array.length;- i++) {
        var item = document.createElement('li');
        var itemButton=document.createElement('button');
        itemButton.style.cssText='background:#f85a5a; border:none; width:200px; height:50px; margin-bottom:50px; align:center; border-radius:25px; color:#ffffff;'
        itemButton.appendChild(document.createTextNode(array[i]));
        item.appendChild(itemButton);
        list.appendChild(item);     
    }
    return list;
}
4
  • Have you tried this.textContent? Please show the click handling function. Commented Mar 8, 2015 at 6:02
  • this.innerHTML doesn't work ? Commented Mar 8, 2015 at 6:06
  • Where do you set .timeButtons class? Commented Mar 8, 2015 at 6:09
  • @dfsq I set it in a div that contains the list. Commented Mar 8, 2015 at 6:09

2 Answers 2

1

Assuming that this is a reference to the button element in question, you can use this.textContent to get the button's text. (Or .innerHTML.)

Demo: http://jsfiddle.net/w0ntsrLx/

Or since in your edited question you seem to be using jQuery, use the .text() method. In a comment you say that the containing div has the "timeButtons" class, so bind a delegated handler to that div as follows:

$(".timeButtons").on("click", "button", function(e) {
    confirmation.push($(this).text());
});

Demo: http://jsfiddle.net/w0ntsrLx/1/

That way the function will only be called if the click is on a button element within the .timeButtons div, and this will be the clicked button. The click handler that you show in your question with $(".timeButtons").click(...) is bound to the div and doesn't in any way test for the buttons, so within the handler this will be the div, not the clicked button.

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

Comments

0

Check this out

Assuming you want pure javascript code,

Whenever an event is triggered, an object is passed back in callback (generally being named as 'event'). this object has many properties including source element, position of click and many more.

  1. get the element using event.srcElement

  2. You can use element.innerHTML or element.innerText to find out the content of the Button.

There is a difference between using innerText and innerHTML, but in your case, both can be used.

Also, you can use jquery too to easily append child, create elements and binding events.

2 Comments

I just tried this.innerText but I get the whole array that populates the list, not the single list item.
Check the jsbin link i have put with the answer. maybe you will get the idea. I am using event.srcElement to get the button reference.

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.