Reputation: 6477
I am missing something simple here. I need to preprocess a textarea before it being posted back to the server. I can link the button to the textarea using Id, but not sure how to do it via class name which would be more reusable.
Code with Id:
<textarea id="Comment" class="CleanHTML"></teaxtarea>
<button type="submit" id="btn" name="btn" value="Save" onClick='document.getElementById("Comment").value = cleanWordClipboard(document.getElementById("Comment").value)'>Save</button>
How would I alter my onClick JS to use the Class "CleanHTML", rather than Id to reference the TextArea?
Upvotes: 0
Views: 211
Reputation: 44841
Instead of document.getElementById('Comment')
, use document.getElementsByClassName('CleanHTML')[0]
getElementsByClassName
returns an HTMLCollection (which can be treated like an array for our purposes), which is why you need the [0]
. If you have multiple elements with that class name, then you obviously need to adjust the index accordingly.
Upvotes: 3