0

I've been trying to swap out a link on a page using javascript and seem to have hit a wall. I believe the way my URL is encoded might be at fault.

var a = document.querySelector('a[href="https://sis.harpercollege.edu/class-schedule/class-schedule.html?course=230&subject=BIO&term=202295&_gl=1*1wa2tu7*_ga*MTA4NzY5ODQ3LjE2MjQ4ODM4NjY.*_ga_2575BXQHNN*MTY0OTY5MTI0NC4yNDYuMS4xNjQ5Njk1NDY0LjA"]');
if (a) {
  a.setAttribute('href', 'https://sis.harpercollege.edu/class-schedule/class-schedule.html?course=130&subject=BIO&term=202295&_gl=1*1wa2tu7*_ga*MTA4NzY5ODQ3LjE2MjQ4ODM4NjY.*_ga_2575BXQHNN*MTY0OTY5MTI0NC4yNDYuMS4xNjQ5Njk1NDY0LjA')
}
<a title="View the schedule for BIO230 for Summer 2022" href="https://sis.harpercollege.edu/class-schedule/class-schedule.html?course=230&amp;subject=BIO&amp;term=202295&amp;_gl=1*3cwusx*_ga*MTA4NzY5ODQ3LjE2MjQ4ODM4NjY.*_ga_2575BXQHNN*MTY0OTY5MTI0NC4yNDYuMS4xNjQ5Njk2MTkzLjA" target="_blank">Summer 2022</a>

1 Answer 1

1

You're trying to select this a[href="https:///sis.harpercollege.edu/class-schedule/class-schedule.html?course=230..... but in your html you have this <a title="View the schedule for BIO260 for Summer 2022" href="https:///sis.harpercollege.edu/class-schedule/class-schedule.html?course=260

Basically The code in your condition will never get executed because query selector doesn't find an a tag with that href attribute value.

Sign up to request clarification or add additional context in comments.

2 Comments

I copied the wrong link, there is a while list of these. I updated my code to reflect that error. I'm still having the same issue though.
Seems like CSS attributes selector doesn't like this "&amp" what I would suggest is using a different approach try to select all the a tags with something like this a[target="_blank"]. Than loop through all of them and compare the href attribute with your link, when they match you have the element you were looking for

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.