0

I am creating small app, I am learning to parse datas from XML to html via Jquery. and I put the datas on list of HTML. But the problem is when I want to click one of the item, always return only the first item.

Example I click the fifth item on list

but again return the first element

Here is my code on html jquery and jquerymobile

        <meta name="viewport" content="widdiv=device-widdiv, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script>    
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.css" />
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function()
            {
              $.ajax({
                type: "GET",
                url: "emertimi.xml",
                dataType: "xml",
                success: function (xml)
                            {
                              $(xml).find("Row").each(function()
                              {
                                $("#content").append("<li><a  href='"+$(this).find("Emri").text()+"'><h2 id='namedet'>"+$(this).find("Emri").text()+" - "+$(this).find("Kuptimi").text()+"</h2><p>"+"</p><p>"+$(this).find("Gjinia").text()+"</p></a></li>");
                              });  
                            },error: function (jqXhr, status, error) {         alert(status + ':' + error + ':' + jqXhr.responseText)     } 

              });

                    $('.lista').click(function(){
                        var href = $('#namedet').html();
                            $('#prove').html(href);

                    });

                });


    </script>
</head>

<body>
    <div id="meshkujt_wrapper" data-role="page">

        <div id="menu_meshkujt" >
            <ul id='content' class="ui-listview ui-listview-inset ui-corner-all ui-shadow lista" data-filter-placeholder="Kërko emrin..." data-role='listview' data-filter='true' data-inset='true'>

            </ul>
        </div>
        <h1 id="prove">prova</h1>
    </div>
</body>

and here is my XML

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <Row>
        <Emri>Abide</Emri>
        <Kuptimi>Adhuruese</Kuptimi>
        <Gjinia>Femer</Gjinia>
    </Row>
    <Row>
        <Emri>Abire</Emri>
        <Kuptimi>Aromë e mirë lulesh.</Kuptimi>
        <Gjinia>Femer</Gjinia>
    </Row>
    <Row>
        <Emri>Adhra</Emri>
        <Kuptimi>Margaritarë i pashpuar</Kuptimi>
        <Gjinia>Femer</Gjinia>
    </Row>
    <Row>
        <Emri>Adile</Emri>
        <Kuptimi>E drejtë</Kuptimi>
        <Gjinia>Femer</Gjinia>
    </Row>
    <Row>
        <Emri>Afife</Emri>
        <Kuptimi>E pastër; E thjeshtë; E lehtë</Kuptimi>
        <Gjinia>Femer</Gjinia>
    </Row>
</Root>

Anyone who can help me, whith this, how to return elemnts data?

1 Answer 1

1

You have an error,

change this:

$('.lista').click(function(){
    var href = $('#namedet').html();
    $('#prove').html(href);
});

to this:

$('.lista li').click(function(){
    var href = $(this).find('#namedet').text();
    $('#prove').html(href);
});

Basically you are telling your code to access first occurrence of element with an id #namedet.

$(this) keyword will look at clicked element and function find will look for an element #namedet inside clicked listview element.

Update:

Working example: http://jsfiddle.net/Gajotres/M26vG/

Use this :

$(document).on('click','.lista li',function(){
    var href = $(this).find('#namedet').text();
    $.mobile.activePage.find('#prove').html(href);
});   
Sign up to request clarification or add additional context in comments.

3 Comments

No its not working again :( I think maybe its problem to the $('.lista').click(function(){ Because class lista is to the ul tag ??
then add $('.lista li'), this should do it.
Now, it doesnt return antyhing...I tried also $('.lista li a')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.