Javascript Question

GetElementById Alternative for more options

Okay, I am new to JavaScript, so I really need help with this.
I have a script, which looks like this:

function Change(){
var image = document.getElementById('id-img');
image.src = "somenotimportantlink" + document.getElementById('id-a1').title;

var link = document.getElementById('id-a2');

My problem is that there are multiple tags that want to use this function on, but I can only use
on one tag, so I need an alternative for getElementById, one that would allow me to use multiple tags.

Answer Source

Most people use jQuery to do this, but you can also do it in JavaScript. The catch is, it only works on modern browsers.

JavaScript Method

You would need to grab a NodeList of all the objects with a query selector and iterate over every item of that NodeList.

function myStuff() {
  var nodeList = document.querySelectorAll('.a');
  for (var i = 0, length = nodeList.length; i < length; i++) {
     nodeList[i].href = "somenotimportantlink"+(i.title)+"/index.html";

jQuery Method

First you'd want to have jQuery somewhere in your HTML, I prefer using a CDN but a normal file is alright.

<script src=""></script>

Once you have the jQuery file ready, you can select all classes by:


or if you want to select all <a> tags: