0

I'm trying to remove a certain element from my array - specifically, .top-header .header-info .header-phone a. At the same time, I'm trying to grab each other instance of .top-header .header-info a Here's my code:

var anchorArray = [];
var titleArray = [];
$('.top-header .header-info a').each(function() {
    var removeItem = $('.top-header .header-info .header-phone a')
    anchorArray.push($(this).attr('href'));
    anchorArray.splice($.inArray(removeItem,anchorArray), 1);
    titleArray.push($(this).html());
    titleArray.splice($.inArray(removeItem,titleArray), 1);
});

<div class="header-info">
  <span class="header-phone"><a href="tel:314-533-1500">314.533.1500</a>&nbsp;or&nbsp;<a href="tel:800-777-2852">1-800-777-ATLAS</a></span>
  <a href="comingsoon">Request a Consultation</a>
  <a href="quickpad">Quick Order</a>
  <a href="contactus">Contact Us</a>
</div>

Currently, this seems to be removing all instances of a links.

Thanks!

4
  • add html as well Commented Mar 18, 2020 at 20:17
  • Added! Whoooooops Commented Mar 18, 2020 at 20:21
  • which specific tag you want to be removed?.top-header .header-info .header-phone a this will remove all of the list. Commented Mar 18, 2020 at 20:32
  • I'm trying to remove that entire span of header-phone from the array, but keeping the remaining three a tags Commented Mar 18, 2020 at 20:36

1 Answer 1

1

A better way for you to do this is to check if the parent div of the element doesnot contain the class you wish to remove. If it doesnt, then add that element.

   <!DOCTYPE html>
    <html>
    <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    </head>
    <body>

    <div class="header-info">
      <span class="header-phone">
      <a href="tel:314-533-1500">314.533.1500</a>&nbsp;or&nbsp;
      <a href="tel:800-777-2852">1-800-777-ATLAS</a></span>
      <a href="comingsoon">Request a Consultation</a>
      <a href="quickpad">Quick Order</a>
      <a href="contactus">Contact Us</a>
    </div>

    <script>
    var anchorArray = [];
    var titleArray = [];
    $('.header-info a').each(function(e,s) {
        if(s.parentNode.className !=="header-phone"){
          console.log(s.parentNode.className);
          anchorArray.push($(this).attr('href'));
          console.log("anchorArray:"+anchorArray);
          titleArray.push($(this).html());
        }
    });

    console.log(titleArray);
    </script>
    </body>
    </html> 
Sign up to request clarification or add additional context in comments.

Comments

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.