942,520 Members | Top Members by Rank

Ad:
You are currently viewing page 1 of this multi-page discussion thread
Mar 31st, 2010
0

To remove li using JQuery

Expand Post »
Hi,

In my application I have <li>s and I am using Jquery to remove a particular <li>

The code I am using is

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. $(".close").click(
  2. function() {
  3. $(this).parent().remove()// Links with the class "close" will close parent
  4.  
  5.  
  6. return false;
  7. }
  8. );


Using this I am successfully able to remove a li but after its being removed...The <li> below it moves in place of the deleted one and is creating a white space. How can i remove that white space
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
sniigg is offline Offline
31 posts
since Mar 2010
Mar 31st, 2010
0

Use CSS display:hidden instead of removing list item

Click to Expand / Collapse  Quote originally posted by sniigg ...
Hi,

In my application I have <li>s and I am using Jquery to remove a particular <li>

The code I am using is

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. $(".close").click(
  2. function() {
  3. $(this).parent().remove()// Links with the class "close" will close parent
  4.  
  5.  
  6. return false;
  7. }
  8. );


Using this I am successfully able to remove a li but after its being removed...The <li> below it moves in place of the deleted one and is creating a white space. How can i remove that white space

try setting the visibility for the list-item to "hidden" instead of removing it if you prefer to have the element occupy the space.
Some thing to the effect
.css('visibility''hidden')
Reputation Points: 10
Solved Threads: 1
Newbie Poster
dsweb1017 is offline Offline
15 posts
since Jul 2009
Mar 31st, 2010
0
Re: To remove li using JQuery
Which element has class="close" , the <li> or something within the <li>?

Airshow
Sponsor
Reputation Points: 299
Solved Threads: 354
WiFi Lounge Lizard
Airshow is offline Offline
2,504 posts
since Apr 2009
Apr 1st, 2010
0
Re: To remove li using JQuery
Hi..

My aspx page has the code

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <ul id="arrangableNodes" runat="server">
  2. </ul>

And My code behind I dynamically generate the li...
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. System.Web.UI.HtmlControls.HtmlGenericControl close = new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");
  2. arrangableNodes.Controls.Add(close);
  3. close.Attributes.Add("class", "notification attention png_bg");
  4. close.Controls.Add((new LiteralControl("<a href='#' class='close'><img src='resources/images/icons/cross_grey_small.png' title='Close this notification' alt='close' /></a>")));
  5. close.Controls.Add(li);
  6. li.ID = strNodeName;
  7. li.Controls.Add(dynDivDesc);

And it is getting removed...but when i remove the li..

the next one moves up creating some white space...and the next li to it gets hidden beneath the first one...
Reputation Points: 10
Solved Threads: 0
Light Poster
sniigg is offline Offline
31 posts
since Mar 2010
Apr 1st, 2010
0
Re: To remove li using JQuery
This is working well in Firefox but creating all these problems in IE
Reputation Points: 10
Solved Threads: 0
Light Poster
sniigg is offline Offline
31 posts
since Mar 2010
Apr 1st, 2010
0
Re: To remove li using JQuery
Mmmmm, it's a bit diffult to interpret the ASPX.

Here's a simple test using longhand HTML/javascript, free of any ASPX/jQuery. I frequently do this sort of extract for cross-browser testing/development of awkward stuff.
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html>
  4. <head>
  5. <title>Airshow :: Untitled</title>
  6. <style type="text/css">
  7. {}
  8. </style>
  9.  
  10. <script>
  11. onload = function(){
  12. var closeLinks = document.getElementsByTagName('a');
  13. var msg = document.getElementById('message');
  14. for(var i=0; i<closeLinks.length; i++) {
  15. closeLinks[i].onclick = function() {
  16. var p = this.parentNode;
  17. var pp = p.parentNode;
  18. //msg.innerHTML = p.parentNode.tagName + ' : ' + p.tagName
  19. pp.removeChild(p);
  20. //msg.innerHTML = pp.childNodes.length;
  21. if(pp.childNodes.length === 0) { pp.style.display = 'none'; }
  22. };
  23. }
  24. };
  25. </script>
  26. </head>
  27.  
  28. <body>
  29. <div id="message">&nbsp;</div>
  30.  
  31. <ul style="width:100px; border:1px solid #999;">
  32. <li><a href="#" class="close">Item 1</a></li>
  33. <li><a href="#" class="close">Item 2</a></li>
  34. <li><a href="#" class="close">Item 3</a></li>
  35. <li><a href="#" class="close">Item 4</a></li>
  36. <li><a href="#" class="close">Item 5</a></li>
  37. </ul>
  38. </body>
  39. </html>
Try commenting out the line if(pp.childNodes.length === 0) { pp.style.display = 'none'; } and you will see that (at least in IE6) the UL block still has some height even after the last LI has been removed. Hence hide it when its childNodes.length === 0 .
Once it's working, you can translate the code back into jQuery if you choose.

Airshow
Sponsor
Reputation Points: 299
Solved Threads: 354
WiFi Lounge Lizard
Airshow is offline Offline
2,504 posts
since Apr 2009
Apr 1st, 2010
0
Re: To remove li using JQuery
Hi Airshow,


I have implemented the way you told me ..but i still have the same problem..no change..
Reputation Points: 10
Solved Threads: 0
Light Poster
sniigg is offline Offline
31 posts
since Mar 2010
Apr 1st, 2010
0
Re: To remove li using JQuery
Which version of IE are you testing in?

Airshow
Sponsor
Reputation Points: 299
Solved Threads: 354
WiFi Lounge Lizard
Airshow is offline Offline
2,504 posts
since Apr 2009
Apr 1st, 2010
0
Re: To remove li using JQuery
IE8...Its working finr with IE7 And Firefox.....
Last edited by sniigg; Apr 1st, 2010 at 8:21 am.
Reputation Points: 10
Solved Threads: 0
Light Poster
sniigg is offline Offline
31 posts
since Mar 2010
Apr 1st, 2010
0
Re: To remove li using JQuery
Seems like an IE8 bug then.

Have you tried Dsweb's suggestion to hide rather than remove the LI?

IE8 may do that OK.

Airshow
Sponsor
Reputation Points: 299
Solved Threads: 354
WiFi Lounge Lizard
Airshow is offline Offline
2,504 posts
since Apr 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: adding web reference in .aspx.cs file
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: IE6/IE7 bug in jQuery/CSS





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC