Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-08-2009, 07:34 AM   PM User | #1
Apothem
Regular Coder

 
Apothem's Avatar
 
Join Date: Mar 2008
Posts: 380
Thanks: 36
Thanked 25 Times in 25 Posts
Apothem is an unknown quantity at this point
Adding text to a list AND removing them

This might be clearer if you use Twitter.
In Twitter, if you post a message, your message slides into the message list. Also, if you click Home (Sidebar), it will find new tweets and merge it into the message list. What I want to know is how do you make it so that jQuery will merge lists together + sort them AND remove older messages afterward.

E.g: Current list:
<li value="100">abc</li>
<li value="68">abc</li>
<li value="32">abc</li>
<li value="5">abc</li>

Queried new:
<li value="80">bcd</li>

New list should become:
<li value="100">abc</li>
<li value="80">bcd</li>
<li value="68">abc</li>
<li value="32">abc</li>
Apothem is offline   Reply With Quote
Old 04-09-2009, 08:43 AM   PM User | #2
Iszak
Regular Coder

 
Iszak's Avatar
 
Join Date: Jun 2007
Location: Perth, Western Australia
Posts: 332
Thanks: 2
Thanked 58 Times in 57 Posts
Iszak is an unknown quantity at this point
Okay, well I made something although it's not exactly what you wanted, it's similar and with some editing I'm sure you can acheive what you want. Although the way I've done this I believe isn't the best and effecient way, it gets the job done. Sorry I couldn't help anymore.

HTML
Code:
<ul id="list">
  <li value="100">100</li>
  <li value="32">32</li>
  <li value="68">68</li>
  <li value="5">5</li>
</ul>

<input id="submit" type="submit" value="Add List Item">
<input id="value" type="text" value="20">
JavaScript
Code:
function sortDesc(a, b)
{
  return (b - a);
}

$('#submit').click(function(){
  var value = $('#value').val();

  $('#list').append('<li value="'+value+'">'+value+'</li>\n');

  list = new Array();

  jQuery.each($('#list > li'), function(){
    list = list.concat($(this).attr('value'))
  });

  list.sort(sortDesc);

  $('#list').children().remove();

  jQuery.each(list, function(){
    $('#list').append('<li value="'+this+'">'+this+'</li>\n');
  });
});
Should be noted I don't believe the value attribute is valid.. so maybe look into something else?

Preview: http://pastebin.me/49dda6757604b
Iszak is offline   Reply With Quote
Old 04-09-2009, 02:52 PM   PM User | #3
Eldarrion
Regular Coder

 
Join Date: Feb 2009
Location: Wheeling, IL
Posts: 358
Thanks: 5
Thanked 62 Times in 60 Posts
Eldarrion is on a distinguished road
Quote:
Originally Posted by Iszak View Post
Okay, well I made something although it's not exactly what you wanted, it's similar and with some editing I'm sure you can acheive what you want. Although the way I've done this I believe isn't the best and effecient way, it gets the job done. Sorry I couldn't help anymore.

Should be noted I don't believe the value attribute is valid.. so maybe look into something else?

Preview: http://pastebin.me/49dda6757604b
The value attribute is valid, but deprecated:

http://www.w3schools.com/tags/tag_li.asp

It should work, and likely will, though you never know when some browser update might crash the website due to the browser not supporting it anymore.
__________________
The way to success is to assume that there are no impossible things. After all, if you think something is impossible, you will not even try to do it.

How to ask smart questions?
Eldarrion is offline   Reply With Quote
Old 04-09-2009, 05:15 PM   PM User | #4
Apothem
Regular Coder

 
Apothem's Avatar
 
Join Date: Mar 2008
Posts: 380
Thanks: 36
Thanked 25 Times in 25 Posts
Apothem is an unknown quantity at this point
Well, how do I add an attribute that is valid and can be used as a reference to sorting?
Apothem is offline   Reply With Quote
Old 04-10-2009, 12:24 AM   PM User | #5
Fumigator
UE Antagonizer


 
Fumigator's Avatar
 
Join Date: Dec 2005
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 7,686
Thanks: 42
Thanked 637 Times in 625 Posts
Fumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of light
You should be able to simply define a Javascript array and use it to do the sorting, then dump the array into the DOM (in the form of your <li> tags) after the sort. The array will remain defined for the life of the page
__________________
Fumigator is offline   Reply With Quote
Old 04-10-2009, 05:42 AM   PM User | #6
Apothem
Regular Coder

 
Apothem's Avatar
 
Join Date: Mar 2008
Posts: 380
Thanks: 36
Thanked 25 Times in 25 Posts
Apothem is an unknown quantity at this point
Yes but what is another way to store a value into a tag? For instance, if I wanted to store the text "300 API? OK" to a <div>, how would I do it?
Apothem is offline   Reply With Quote
Old 04-10-2009, 02:34 PM   PM User | #7
Eldarrion
Regular Coder

 
Join Date: Feb 2009
Location: Wheeling, IL
Posts: 358
Thanks: 5
Thanked 62 Times in 60 Posts
Eldarrion is on a distinguished road
Quote:
Originally Posted by Pykex View Post
Yes but what is another way to store a value into a tag? For instance, if I wanted to store the text "300 API? OK" to a <div>, how would I do it?
I guess if you _must_ you can use the title attribute to store info. Or well, in a <div> you can just stick what you want in the div contents, but eh... I guess that won't work if there's something else in there. All in all... one way to do it is making a hidden input field inside the div that would contain your data, but that might be too much trouble.
__________________
The way to success is to assume that there are no impossible things. After all, if you think something is impossible, you will not even try to do it.

How to ask smart questions?
Eldarrion is offline   Reply With Quote
Old 04-10-2009, 05:07 PM   PM User | #8
Fumigator
UE Antagonizer


 
Fumigator's Avatar
 
Join Date: Dec 2005
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 7,686
Thanks: 42
Thanked 637 Times in 625 Posts
Fumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of light
I'm not sure I get the point of trying to turn HTML elements into storage areas. I'm not saying it shouldn't be done, but if there were a point to doing it, I'd love to know what that point is.
__________________
Fumigator is offline   Reply With Quote
Old 04-10-2009, 05:14 PM   PM User | #9
Apothem
Regular Coder

 
Apothem's Avatar
 
Join Date: Mar 2008
Posts: 380
Thanks: 36
Thanked 25 Times in 25 Posts
Apothem is an unknown quantity at this point
One thing it can be used for is for resizing a bunch of images, as such:
Code:
<img src="http://link/to/my/image1.jpg" original="1280px" />
<img src="http://link/to/my/image2.jpg" original="2000px" />
<img src="http://link/to/my/image3.jpg" original="1600px" />
.... and so on.
All those images are extremely big so I use jQuery to resize them back to about 700px. As a result, I also allow the user to click the image to resize to its original width... which requires me to somehow log the original width.

That's kind of why I want to know how to store data.... INCLUDING if it's something which has no innerHTML (or $.html()).
Apothem is offline   Reply With Quote
Old 04-10-2009, 08:13 PM   PM User | #10
Fumigator
UE Antagonizer


 
Fumigator's Avatar
 
Join Date: Dec 2005
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 7,686
Thanks: 42
Thanked 637 Times in 625 Posts
Fumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of light
I feel like we're going in circles because you can simply use Javascript variables and arrays to store stuff.
__________________
Fumigator is offline   Reply With Quote
Old 04-10-2009, 09:29 PM   PM User | #11
Eldarrion
Regular Coder

 
Join Date: Feb 2009
Location: Wheeling, IL
Posts: 358
Thanks: 5
Thanked 62 Times in 60 Posts
Eldarrion is on a distinguished road
I'd personally have to agree with Fumigator there that storing the information in question in an array would be much easier to do than pulling it out of attributes like title and such... plus, if you put both dimensions in one string, you'll be needing to parse through that string to pull them out, and, and... well, it sounds like you're trying to cut bread with a chainsaw.
__________________
The way to success is to assume that there are no impossible things. After all, if you think something is impossible, you will not even try to do it.

How to ask smart questions?
Eldarrion is offline   Reply With Quote
Old 04-12-2009, 05:12 PM   PM User | #12
Apothem
Regular Coder

 
Apothem's Avatar
 
Join Date: Mar 2008
Posts: 380
Thanks: 36
Thanked 25 Times in 25 Posts
Apothem is an unknown quantity at this point
Yes... but that still doesn't answer my question. Thanks for suggesting alternatives, but I don't want to create 2+ arrays just to store information about 20 or so elements.
Apothem is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:22 PM.


Advertisement
Log in to turn off these ads.