kiran
kiran

Reputation: 45

read span tag value from the string html using javascript

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>
   

Upvotes: 0

Views: 1004

Answers (3)

Uttam Gupta
Uttam Gupta

Reputation: 418

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]);

Upvotes: 0

Guy Louzon
Guy Louzon

Reputation: 1203

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
    } 
    } 

Upvotes: 0

Jai
Jai

Reputation: 74738

.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>

Upvotes: 2

Related Questions