7

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.

5
  • 3
    Can't you give your <li> elements meaningful names or data-attributes when parsing the JSON file instead of mucking with the rendered HTML?
    – millimoose
    Jun 21, 2012 at 18:51
  • Also: please use English identifiers in code samples.
    – millimoose
    Jun 21, 2012 at 18:54
  • 3
    @millimoose: Does it really matter what the variable is named?
    – gen_Eric
    Jun 21, 2012 at 18:54
  • I've edited your question title, I hope it rephrases your request better.
    – Bergi
    Jun 21, 2012 at 18:55
  • @Rocket Seeing as it's the main clue as to the purpose of a variable, yes it does. Perhaps not for this question seeing as the code sample was very short, but I intended the comment as general advice to a new user.
    – millimoose
    Jun 21, 2012 at 19:11

3 Answers 3

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

Regex is our friend :)

0
3

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

2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.