question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Having multiple classes on textarea or button doesn't work

See original GitHub issue

There’s a problem with having multiple classes on the textarea or button.

Easy to reproduce in the sample index.html by adding a class (“other” in example) to the textarea:

<textarea rows="5" class="other text-to-copy-0">Create as many elements as you want, and match them up by name.</textarea>
<p>
  <button class="js-copy-btn-0">Copy text</button>
</p>

This will result in the following error:

Uncaught Error: You don't have a <button> with the class: 'js-copy-btn'. Please check the cheval README.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
mattiasjahnkecommented, Dec 23, 2016

@ryanpcmcquen yepp seems to be working just fine (tested it in my own project aswell).

Great job, dude!

A slightly more elegant solution than mine 😉

var buttons = Array.prototype.slice.call(document.querySelectorAll(
      '[class*=js-copy-btn]'));

var list = [];
for (var i = 0; i < buttons.length; i++) {
   var classNames = buttons[i].classList;
   for (var ic = 0; ic < classNames.length; ic++) {
        var match = classNames[ic].match('js-copy-btn');
        if (match != undefined) {
          var replaced = match.input.replace(/js-copy-btn/, '');
          var textMatch = ''
          if (replaced === '') {
            textMatch = '.text-to-copy'
          } else {
            textMatch = '.text-to-copy' + replaced;
          }
          var textObj = document.querySelector(textMatch);
          list.push({'btn' : buttons[i], 'txt' : textObj});
       }
  }
}

My js-skills are very limited so I had to improvise ;D But I look forward to an updated version so I can throw my hack out the window.

0reactions
ryanpcmcquencommented, Dec 24, 2016

No worries, thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

add different texts to textarea by button - not working
First, in your html, you need to call your event onclick. Second, you forgot to close the ) like this:
Read more >
The Textarea element - HTML: HyperText Markup Language
This attribute enables you to place <textarea> elements anywhere within a document, not just as descendants of form elements.
Read more >
Input group - Bootstrap
Easily extend form controls by adding text, buttons, or button groups on either side of textual inputs, custom selects, and custom file inputs....
Read more >
How To Modify Attributes, Classes, and Styles in the DOM
In the code below, there are two div elements with a common class ... will return HTML collections which do not have access...
Read more >
How to change the Content of a textarea using JavaScript
Method 2: This method uses the id attribute of the textarea with innerHTML property to change the content of <textarea> element.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found