This might seem like an odd request but I'd like to use jQuery's Selectable tool to only select one item at a time and I'd like it to show me a value I'll have within each tag. At the very least I want the contents of that selection. Has anyone tried to do this? For some reason these little things seem to not be all that easily findable in their API for it.
A:
A post from the jQuery Forum offers the following code to limit selection to a single item:
$("li").live("click", function(event) {
$(this).siblings().removeClass("selected");
$(this).addClass("selected");
});
As far as extracting data, here is the code that the jQuery UI team uses in the Serialize Demo, to extract data from each selected element:
$("#selectable").selectable({
stop: function(){
var result = $("#select-result").empty();
$(".ui-selected", this).each(function(){
var index = $("#selectable li").index(this);
result.append(" #" + (index + 1));
});
}
});
And the HTML markup is:
<ol id="selectable">
<li class="ui-widget-content">Item 1</li>
<li class="ui-widget-content">Item 2</li>
<li class="ui-widget-content">Item 3</li>
<li class="ui-widget-content">Item 4</li>
<li class="ui-widget-content">Item 5</li>
<li class="ui-widget-content">Item 6</li>
</ol>
Justin Ethier
2010-06-07 13:25:31
That's awesome thanks, I'll give that a shot
jphenow
2010-06-07 13:32:31
A:
If you only want one selection at a time, then why use selectable?
I would think you would just use jQuery, as it should be very easy. Or am I missing your point?
Live Example: http://jsfiddle.net/F7fsx/
HTML
<div id='container'>
<div id='one'>one</div>
<div id='two'>two</div>
<div id='three'>three</div>
</div>
CSS
#container div {
width: 50px;
height: 50px;
border: 2px dashed red;
margin: 10px;
color: white;
background:orange;
}
jQuery
$('#container div').click(function() {
$th = $(this);
$th.css('backgroundColor','yellow');
var value = $th.text()
alert(value);
});
patrick dw
2010-06-07 13:29:52
Ha Yea I now have no clue why I didn't just do this real quick. I'll give yours or the answer below both a shot and see which I like better. Thanks! Wait will $th.text work if I'm inserting that selected section from asp codebehind? I think that's why when I try to get the value right now it looks like garbled crap
jphenow
2010-06-07 13:34:25
@jphenow - `$th.text()` is simply getting the text content of the element that was clicked. It presumes you are clicking an existing element with existing content. Shouldn't matter where it came from. Just a generic example, as I don't know your specific situation. Try the live example I posted.
patrick dw
2010-06-07 13:38:01
@jphenow - If you meant the elements are added dynamically, you'll need to use `live(...)` instead of `click(...)`, as in `$('#container div').live('click', function() {...`
patrick dw
2010-06-07 13:40:55
Wow thank you for telling me that last part. I feel like a chicken with my head cut off trying to pair ASP with jQuery. It might have been a poor decision but I've gotten so far into it - it felt like I shouldn't turn now - that little note of info will probably save me a lot.
jphenow
2010-06-07 13:53:39
@jphenow - I don't know ASP, but I don't know of any reason why it would be a bad pairing. Elements added after the page is loaded will have the same issue, irrespective of their source. Were you able to get your code working?
patrick dw
2010-06-07 14:11:11
I just realized I wasn't using parenthesis after .text.... can't believe it.
jphenow
2010-06-07 14:48:39