-1

I have a checkboxlist where each item has an id. When the text of an item is clicked the id("cssId") should be printed out in console.log.

Checkboxlist:

<div id="cssListBox" class="list-group">
            <ul id="listItemCss">
                @foreach (var item in Model.CssTemplates)
                {
                    <li id="itemInList">
                        @Html.CheckBox("ChoiceBox", item.IsConnected)
                        @Html.Hidden("cssId", item.Id, new { @class = "cssId" })
                    </li>
                }
            </ul>

        </div>

Jquery:

$('#listItemCss>li').click(function() {
        var cid = (this).val();

        console.log('cid');
    });

Please tell me if I can make the question more clear in some way, I am a bit slow so have patience.

FadiY
  • 119
  • 2
  • 10
  • could you post the html that get's outputted rather than the text for the cshtml file please. – George Jun 02 '15 at 12:25

3 Answers3

0
$('#listItemCss>li').click(function() {
    var cid = $(this).find('.cssId').val();
    console.log(cid);
});

Also, don't use the same id for multiple li elements

DEMO

See Why is it a bad thing to have multiple HTML elements with the same id attribute?

Community
  • 1
  • 1
AmmarCSE
  • 30,079
  • 5
  • 45
  • 53
  • @FadiY, no it has nothing to do with it. Perhaps I misunderstood what you want. Do you want the value of cssId? – AmmarCSE Jun 02 '15 at 12:39
  • Yes I want value of cssId. – FadiY Jun 02 '15 at 12:44
  • Now log says "false". I know I get Id because I can see generated items in browser. – FadiY Jun 02 '15 at 12:57
  • @FadiY, can you reproduce it in a fiddle? – AmmarCSE Jun 02 '15 at 13:08
  • Sorry I cant do that since
  • are database objects.
  • – FadiY Jun 02 '15 at 13:12
  • @FadiY, just take the static html produced by the razor and put it in a fiddle. – AmmarCSE Jun 02 '15 at 13:13