How to find value in sorting using javascript

I have a sort where it was created with loading from JSON files. Now I want to delete the item.

I get the name of the element from the text box that I have to undo. I save the namdel variable. With the for loop, I'm going to compare this variable with the name of the sortable.

HTML code of the sortable:

 <div id="sortparam"> <ul style="" class="ui-sortable" id="sortable"> <li style="" id="1" class="ui-state-default"> <span class="ui-icon ui-icon-arrowthick-2-ns"></span>Singular sensation</li> <li style="" id="2" class="ui-state-default"> <span class="ui-icon ui-icon-arrowthick-2-ns"></span>Beady little eyes</li> <li style="" id="3" class="ui-state-default"> <span class="ui-icon ui-icon-arrowthick-2-ns"></span>Little birds </li> </ul> </div> 

The problem is how to read the elements, because if I read with:

 var contapara=1; var l = document.getElementById(contapara).innerHTML; alert(l); 

The program is written to the warning window:

 <span class="ui-icon ui-icon-arrowthick-2-ns"></span>Little birds 

I want only Little birds .

+6
source share
3 answers
 var contapara=1; var regex = /(<([^>]+)>)/ig; var l = document.getElementById(contapara).innerHTML.replace(regex, ""); alert(l); 

Regex is our friend :)

+3
source

Try the following:

 var contapara = 3; var l = $.trim($('#'+contapara).text()); alert(l); // Little birds 

Instead of using document.getElementById I use jQuery to get the element. I also use .text() ( innerText or textContent ) instead of .html() ( innerHTML ).

+3
source

Using plain javascript (tested in chrome, IE, FF and opera)

 var contapara=3; var n = document.getElementById(contapara).childNodes; for(i=0;i<n.length;i++) { if(n[i].nodeType==3 && n[i].nodeValue!=' ') alert(n[i].nodeValue); } 

Demo.

+2
source

Source: https://habr.com/ru/post/918689/


All Articles