-1

I am trying to capture the text value of a parent class when a click event happens . In this case the parent class ame is mb-name and will have a "text value" associated with it. I want to capture that text. below is the html.

<div class="mbs-item-inner">
   <div class="mb-img">,,,</div>
   <h5 class="mb-name">Joe Banker</p>
   <p class="mb-title">Banker</h5>
   <p class="mb-loc">Virginia Beach, VA</p>
   <p class="mb-view">
     <a href = "......." class= "btn-ghost-blue" target="_blank">VIEW ME</a> ==          $0
</p>

the click event happens on the class = mb-view.. I want to capture the class = mb name "in this case Joe Banker". I tried the following

document.getElementByClassName("mb-name").text;
return true;

and a few other variants of document.getElementssbyClassName but nothing works. I welcome all suggestions.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Darius
  • 85
  • 2
  • 4
  • 12

1 Answers1

-1

This piece of code should do what you want.

    document.getElementsByClassName('mb-name')[0].innerHTML;

I'd advise you to clean up your HTML a bit. You're starting a h5-tag which ends with a p-tag and vice versa.

Had you been using jquery you could to the same like this:

    $('h5.mb-name').text();
Ondemannen
  • 19
  • 5
  • The HTl was a rush , but its just fine on the site, I am not the coder for the site. I just try to hurry and type what element I was concerned with, not paying attention to syntax etc.. I will try your suggestion. Thank you for your response – Darius Jan 03 '17 at 00:31
  • so i did the following.. var name = document.getElementsByClassName('mb-name')[0].innerHTML; return name; .... The problem is, that it is bubbling up and grabbing the mb-name from the very first Element in the list. In other words the html code I post above represents just one of many of the same in a list.. so instead of grabbing the mb-name associated with the actual click event it bubbles up to the top of the list and grabs the very first mb-name.. How do I get around this? – Darius Jan 03 '17 at 03:13
  • Then you'll need to make sure that the click-event is set up for its own parent object. Like this [jsfiddle](https://jsfiddle.net/0tLyy0e2/3/) – Ondemannen Jan 03 '17 at 14:19