0

I have something like this:

function (foo) {

}

Foo contains some HTML. For example:

<div>
    <div>
      <div class="myclass>
        <div> 
          Hello World
        </div>
      </div>
    </div>
</div>

I need to extract that info off the variable somehow.

I have tried:

function (foo) {
    $('.myclass > script').text()
}

I probably need some way to select foo instead. Any help?

1
  • Not very clear what you are trying to ask here. What non-existing script element that is a child of .myclass are you trying to select here? Commented Apr 16, 2020 at 8:39

1 Answer 1

1

If you want to do this in jQuery then you need to convert the string of HTML held in foo in to a jQuery object which you can interrogate with jQuery methods to retrieve the data you need. For example:

function getText(foo) {
  return $(foo).find('.myclass').text();
}

let input = '<div><div><div class="myclass"><div> Hello World</div></div></div></div>';
console.log(getText(input));
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

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.