views:

66

answers:

2

I have a ul list, with 10 items, and I have a label that displays the selected li text.

When I click a button, I need to check the label, versus all the list items to find the matching one, when it finds that I need to assign the corresponding number.

I.e.

list: Messi Cristiano Zlatan

hidden values of list items: www.messi.com www.cronaldo.com www.ibra.com

label: Zlatan

script (thought)procces: get label text, search list for matching string, get the value of that string.

(and if someone could point me in a direction to learn these basic(?) stuff.

tried to be as specific as possible, thanks guys!

edit: really sorry for not being clear.

the li's are all getting a class dynamically (.listitem). the links can be stored in an array or in a hidden field (or other ul thats hidden) it doesn't really matter where the links are..

$('.listitem').click(function() {
$("#elname").text($(this).text());
$("#such").attr("href", $(this).attr('value'));
});

I was trying that with the li's having values but I realized that li's can't have values..

thanks again!

+1  A: 

I think this should work:

var label = "Zlatan";
var url = ul.find("li")
    .filter(function(){return $(this).text() == label})
    .find("input[type=hidden]")
    .val();

(Here I guess you mean hidden input field, because a li can't have a hidden value).

edwin
+2  A: 
<ul>
    <li title="www.messi.com">
        Messi 
    </li>
    <li title="www.cronaldo.com">
        Cristiano
    </li>
    <li title="www.ibra.com">
        Zlatan
    </li>
</ul>
<label id="selected"></label>
<br />
<br />
<input type="button" id="button" value="click" />

<script type="text/javascript">
    $(document).ready(function() {
        $("li").each(function(i) {
            $(this).click(function() {
                $("label").html($(this).html());
            });
        });

        $("#button").click(function() {
            alert($("li:contains(" + $("label").html() +")").attr("title"));
        });
    });
</script>

I store the hidden fields values in the titles of li elements to simplify my work

anilca
I used your fantasy and wrote this: $("#such").attr("href", $(this).attr('title')); . Really helpful, thanx a bunch! Half accepting this :)
Noor
You're welcome! :)
anilca
That should be `$("#selected")` to avoid issues with other `label` elements on the page. Also, `each` is not necessary. You can just do `$("li").click(...);'
Justin Johnson
You're right Justin. Thanks a lot
anilca