-2

I have such code:

    <li class="myClass">
        <a href="www.example.com">link</a>
    </li>

I need to change href attribute to www.example2.com How would I achieve that using javascript?

jbmnt
  • 11
  • 4
  • If you want to change the `href` attribute of *multiple* elements, see this answer - http://stackoverflow.com/a/28016553/2680216 – Josh Crozier Mar 24 '15 at 21:04

1 Answers1

0

You could use document.querySelector:

document.querySelector('.myClass a').href = 'www.example2.com'

Javascript was originally designed to access the DOM on websites, so you would need to find the element within document. There are a bunch of ways to do it. Check out all the methods that start with "get" here: http://www.w3schools.com/jsref/dom_obj_document.asp.

KJ Price
  • 5,774
  • 3
  • 21
  • 34