Link to home
Start Free TrialLog in
Avatar of Moti Mashiah
Moti MashiahFlag for Canada

asked on

Jquery

Hi guys,
I have jquery code and I want to do on click button go to my ul and grab my first li.

i did something like this which is not workin:

<ul id="myulid">
    <li>Coffee</li>
    <li>Tea</li>
    <li>Milk</li>
</ul>

<input type="button" id="btnid" value="clickMe">

<div id="addone">

</div>




@section scripts{
    <script>
        $(document).ready(function () {
            debugger;
            $("#btnid").click(function () {
                var test = $("#myulid").select("li:first");
                $("#addone").append(test.text());
            });

        });
    </script>

}

Open in new window

Avatar of Moti Mashiah
Moti Mashiah
Flag of Canada image

ASKER

its bringing all the li text.
Avatar of Leonidas Dosas
$('#btnid').on('click',function(){
  var test = $('li').first();
  $("#addone").append(test.text());
});

Open in new window

Tested in liveweave
Leonidas,

Thank you for your answer. can you give me example with select as well?
like  var test = $("#myulid").select("li:first");
$('#btnid').on('click',function(){
  var test = $('li:first');
  $("#addone").append(test.text());
})

Open in new window

I can't do it that why as I have many ul in this page:

Please can you correct my syntax:
I have to choose the ul id - myulid then the il.
<ul id="myulid">
    <li>Coffee</li>
    <li>Tea</li>
    <li>Milk</li>
</ul>
 $("#btnid").click(function () {
                var test = $("#myulid").select("li:first");
                $("#addone").append(test.text());
            });
$('#btnid').on('click',function(){
  var test=$('ul#myulid li:first');
  $("#addone").append(test.text());
  })

Open in new window


In this code you can choose the ul via the id.
Thank you that is working just fine,but still I would be happy to have something with "select"

like var test = $("#myulid").select('li:first')

I don't know why this is not working for me.

Thanks,
ASKER CERTIFIED SOLUTION
Avatar of Leonidas Dosas
Leonidas Dosas
Flag of Greece image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I did it many times before and it works so anyways I will use the code you gave me thank you.
btw, I can't click on best solution as it grays out.
Do you have an idea why ?
k, thank you.