Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

177
Views
how to get the <a>under the <p>

I'm trying to get the a tag under the specific p tag.

I have tried this but is not working. I am trying to change the CSS style of the link part to red color and came with a red dot line when hovering the line will become solid.

var font_8 = document.getElementsByClassName('font_8');var elements = font_8.getElementsByTagName('a');

html:

    <p class="font_7" style="font-size:16px">text text text text<br>
<span style="text-decoration:underline"><a href="URL-link" target="_blank" rel="noreferrer noopener">link link link</a></span>(text)</p>
    <p class="font_8" style="font-size:16px">text text text text<br>
<span style="text-decoration:underline"><a href="URL-link" target="_blank" rel="noreferrer noopener">link link link</a></span>(text)</p>

js:

 <script>
    const styles = {
      textDecoration: 'none',
      color: 'red',
      borderBottom: '2px dotted currentColor',
    }
    
    function getElements() {
      var font_8 = document.getElementsByClassName('font_8');var elements = font_8.getElementsByTagName('a');
      var len = elements.length;
      for (let i = 0; i < len; i++) {
          Object.keys(styles).forEach((key)=>{
            elements[i].style[key]=styles[key]
          })
      }
      for (let i = 0; i < len; i++){
          elements[i].addEventListener('mouseover', function() {
            elements[i].style.borderBottom= '2px solid currentColor';
          })
          elements[i].addEventListener('mouseout', function() {
            Object.keys(styles).forEach((key)=>{
              elements[i].style[key]=styles[key]
            })
          })
      }
    }
    
    getElements()
    </script>
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Using JavaScript just to learn, this is my solution with a query selector:

const styles = {
  textDecoration: 'none',
  color: 'red',
  borderBottom: '2px dotted currentColor',
}
const normalStyle = 'dotted'
const hoverStyle = 'solid'

function applyStylesAndHover() {
  // querySelectorAll allows you to basically use a CSS selector in your JS
  const elements = document.querySelectorAll('.font_8 a');

  elements.forEach((element) => {
    Object.keys(styles).forEach((key) => {
      element.style[key] = styles[key]
    })
    element.addEventListener('mouseover', function() {
      element.style.borderBottomStyle = normalStyle;
    })
    element.addEventListener('mouseout', function() {
      element.style.borderBottomStyle = hoverStyle;
    })
  })
}
applyStylesAndHover()
<p class="font_7" style="font-size:16px">
  text text text text
  <br>
  <span style="text-decoration:underline">
  <a href="URL-link" target="_blank" rel="noreferrer noopener">link</a>
  </span>(text)
</p>
<p class="font_8" style="font-size:16px">
  text text text text
  <br>
  <span>
  <a href="URL-link" target="_blank" rel="noreferrer noopener">link</a>
  </span>(text)
</p>

Although To style the <a> on hover you do not need JavaScript. CSS is perfect for this:

.font_8 a { /* An <a> inside of an element with the class of font_8 */
  color: red;
  text-decoration: none;
  border-bottom: 2px solid red;
}

.font_8 a:hover { /* An <a> that is being hovered inside of an element with the class of font_8 */
  border-bottom-style: dotted;
}
<p class="font_7" style="font-size:16px">
  text text text text
  <br>
  <span style="text-decoration:underline">
  <a href="URL-link" target="_blank" rel="noreferrer noopener">link</a>
  </span>(text)
</p>
<p class="font_8" style="font-size:16px">
  text text text text
  <br>
  <span>
  <a href="URL-link" target="_blank" rel="noreferrer noopener">link</a>
  </span>(text)
</p>

about 4 years ago · Juan Pablo Isaza Report

0

One problem i found here is you are tring to get <a> using font_8.getElementsByTagName('a'); which will not work. u will need to assign id to the a tag and get it by using

var element = document..getElementsById('myAncor');

and To move it inside p tag u will need to append the element to font_8 using .appenChild().

edit: sry i misunderstood ur question.

to do this with JS use

var element= font_8[0].children[1].children[0];
element.addEventListener(
 // your core
)

or

var elements= document.querySelectorAll('.font_8 a');
//Your code
elements[i].addEventListener(
 // your core
)
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!