Posted in javascript
1:59 am, June 17, 2020

get element by class name with javascript and change it function

You may have seen the get element by id in javascript, which is great when there is only one element but what if you need to get the element using its class name

The issue with classes is that they can be re-used so may not be unique.

Using the following code will return an array of the class elements that it finds, and then you can access them like an array or javascript object.

HTML

<div class='shared_class'>Hi Im A Shared Class 1</div>
<div class='shared_class'>Hi Im A Shared Class 2</div>
<div class='shared_class mb-3'>Hi Im A Shared Class 3</div>
<button class='btn btn-primary' onclick='change_second_text();'>Change Second Item</button>
<button class='btn btn-primary' onclick="change_any_text(0, 'Moo');">Change 1 (0) to Moo</button>
<button class='btn btn-primary' onclick="change_any_text(2, 'Moo Three!');">Change 3 (2) to Moo Three!</button>

Javascript

// here is a basic version
function change_second_text() {
  var shared = document.getElementsByClassName("shared_class")[1].innerHTML = "<b>Yay i changed...</b>";
}

// here is a function with some more options
// this one you can pass in the item number and the text replace it with on the function
function change_any_text(item_num, text) {
  var shared = document.getElementsByClassName("shared_class")[item_num].innerHTML = text;
}
Hi Im A Shared Class 1
Hi Im A Shared Class 2
Hi Im A Shared Class 3

No Items Found.

Add Comment
Type in a Nick Name here
 
Other Items in javascript
ajax callback to delete with sweet alert confirmation to make it cool! 😎 prevent default click event on link show the browsers name in javascript json test objects (or arrays) loop through a complex object json javascript load json with vanilla javascript no jquery set active class based on url value check if a variable is undefined or NULL fancybox image popout easy check box highlight text area css validate an email address from a form field - version 2 check if a checkbox is checked with javascript form submit override with javascript submit a form with javascript onclick dropdown change input box on select Slick Slider Carousel with Fade Transition clear a input field when it looses focus using onblur javascript page redirect page scroll indicator research replacement slider for ken wheeler's slick carousel tiny slider 2 [testing] change a select box back to its initial select value select onchange add years as options to select dropdown using moment.js to make your time format nicer loop through object json javascript set the html of an element javascript get the value of an input form element (input value) minutes to decimal calculator javascript version check if a certain query string is set then apply it to all url's on the page check if a query string item is set in the current url append a query string to all url's on a page slick carousel jump to a slide on element hover Adding HTML Templates into TinyMCE Editor How to set a cookie and expire it in 30 days enable dark mode in tinymce codemirror htmlmixed textarea darkmode toggle switch with local storage to remember the last selection checking the current url and then marking a link active based on that url set maxHeight of element by id with javascript How to add jquery to squarespace site Modern HTML5 Techniques for changing classes vimeo thumbnail generator play a sound every # of seconds Monitoring Visible Objects on Screen check that a number is in the correct range based on a form selection how to install js socials for social media share buttons and make them rounded using javascript to open a link in the same window and in a new window or tab Leaflet Quick Start Example country select javascript check which option is selected and show a hidden element country select javascript check which option is selected
Page Views

This page has been viewed 1074 times.

Search Code
Search Code by entering your search text above.
Welcome

This is my test area for webdev. I keep a collection of code snippits here, mostly for my reference. Also if i find a good site, i usually add it here.

âĪðŸ‘Đ‍ðŸ’ŧðŸ•đ

Random Quote
There is a qualitative and quantitative difference between a day that begins with a little exercise, a book, meditation, a good meal, a thoughtful walk, and the start of a day that begins with a smartphone in bed.
Unknown