0

I want to get the value of span which coming from HTML string. The sample code link- http://jsfiddle.net/9cCHy/58/

 var mystring = '<div class="table-cell"> <span>test</span>            </div> <span id="isbool" style="display:none">True</span></div>';

    var after = $('<div/>').html(mystring).find("span[id='isbool']").contents().unwrap().end().end().html();
    // want to capture value true without any dependency of html
    alert(after);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
   

1
  • 1
    @rv7 <div/> is absolutely fine. This is a simple tag for jquery to create a div element. Same way you can create other elements too like <span/>, <table/> etc. Commented Oct 5, 2018 at 7:50

3 Answers 3

2

.text() method can give you the text content of the target element:

var mystring = '<div class="table-cell"> <span>test</span>            </div> <span id="isbool" style="display:none">True</span></div>';

var after = $('<div/>').html(mystring).find("span[id='isbool']").text();
// want to capture value true without any dependency of html
console.log(after);
<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

0

if you have a class on the parent tag, you can loop through its children:

    var div0 = document.getElementsByClassName('table-cell')
    var children0 = div0[0].children;
    for (i = 0; i < children0.length; i++) {
     if (children[i].tagName = 'span') {
  var innerh = children[i].innerHTML; 
    alert('true:' + innerh);
    break
    } 
    } 

Comments

0

you can try below code

var mystring = '<div class="table-cell"> <span>test</span>            </div> <span id="isbool" style="display:none">True</span></div>';    
$ele = $.parseHTML(mystring); 
alert($ele[2]);

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.