0

what I want something like this

var newDiv = document.createElement("div");
        newDiv.innerHTML = data;
	var List=newDiv.$(".myclass h4");
    console.log($(List[0]).html());

but it doesnot work.

1
  • Did you mean $(List).html() instead of $(List[0]).html()? What “first element” are you trying to access? What precisely does “doesn’t work” mean? Commented Jul 10, 2018 at 0:59

2 Answers 2

1

If you make that newDiv a jQuery object, and then use find(), you can get the other element within it.

$(newDiv).find(".myclass h4");

Stack snippet

var data = "<div class='myclass'><h4>hello</h4></div>"
var newDiv = document.createElement("div");
newDiv.innerHTML = data;
var List = $(newDiv).find(".myclass h4");
console.log( $(List).html() );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Sign up to request clarification or add additional context in comments.

Comments

1

An alternative is passing as the second parameter the created element as the parent/source DOM element to find elements from it.

$(".myclass h4", newDiv);  <--- This call returns a jQuery object.
                 ^
                 |
                 +--- Parent/Source element

var newDiv = document.createElement("div");
newDiv.innerHTML = '<div class="myclass"><h4>Ele from Stack</h4></div>';

var list = $(".myclass h4", newDiv);
console.log(list.html());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

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.