Just beginning my journey with JS, looking to tackle an issue (I know, everyone says that!). My task is to create a script that can choose an anchor element and inject an <img>
.
Nested within a div with the id of _nwa_social_logins
, there are multiple <a>
tags. I need to append an <img>
tag to one of these <a>
elements. Not quite sure on the most effective approach for this.
This pertains to a captive portal page generated by a product we utilize, and my area of focus resembles the following snippet:
<div id="_nwa_social_logins">
<a class="btn btn-facebook" href="<url>/guest/social-media.php?oauth_init=1&oauth=facebook" title="">
<i class="icon-facebook"></i> Facebook
</a>
<a class="btn btn-linkedin" href="https://<url>/guest/social-media.php?oauth_init=1&oauth=linkedin" title="">
<i class="icon-linkedin"></i> LinkedIn
</a>
<a class="btn btn-google" href="https://<url>/guest/social-media.php?oauth_init=1&oauth=google" title="">
<i class="icon-google"></i> Google
</a>
<a class="btn btn-twitter" href="https://<url>/guest/social-media.php?oauth_init=1&oauth=twitter" title="">
<i class="icon-twitter"></i> Twitter
</a>
<a class="btn btn-github" href="https://<url>/guest/social-media.php?oauth_init=1&oauth=azure" title=""><img src="images/icon-microsoft.png" align="middle">Microsoft Azure AD
</a>
<a class="btn btn-github" href="https://<url>/guest/social-media.php?oauth_init=1&oauth=amazon" title="">Amazon</a>
</div>
At this point, I've got
let items = document.querySelector('#_nwa_social_logins a');
To be honest, I'm unsure how to verify if anything has been selected.
The goal is to target the Amazon anchor and include an <img>
for the button. If the aforementioned selection yields a collection of <a>
elements, can I iterate through them to identify one with an 'amazon' ending in the href attribute?
Appreciate your help!