Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find value's item in sortable with javascript

I have a sortable where it was created with loaded from JSON files. Now I want to delete an item.

I receive from a textarea the element name that I have to cancel. I save in a variable namdel. With a for loop I am going to compare this variable with the name of the sortable.

The 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-n-s"></span>Singular sensation</li>
    <li style="" id="2" class="ui-state-default"> <span class="ui-icon ui-icon-arrowthick-2-n-s"></span>Beady little eyes</li>
    <li style="" id="3" class="ui-state-default"> <span class="ui-icon ui-icon-arrowthick-2-n-s"></span>Little birds </li>
</ul>

</div>

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

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

The program write in alert window:

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

I want only Little birds.

like image 960
Mirko Cianfarani Avatar asked Jun 21 '12 18:06

Mirko Cianfarani


2 Answers

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

Regex is our friend :)

like image 178
totten Avatar answered Oct 27 '22 02:10

totten


Try this:

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

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

like image 42
Rocket Hazmat Avatar answered Oct 27 '22 01:10

Rocket Hazmat