0

Hi I am trying to get attribute values from array of elements...

JQuery

   function sendM(){
    $.ajax(
        {
            type:'POST',
            url:'../business_logic/send_chat_user.php',       //URL
            data:{ u_id: <?php echo $_SESSION['uid']?>,c_id },  //Data

            beforeSend:function(){},
            success:function(data,status){                      //Data,status
            var item = $(data).hide().slideDown("slow");
            var el = $.parseHTML(data);
            var e = $("a", el).attr ("value"); // This only gives me single value.. I want an array

            $('#msg').append(item);
            }
        });
}

send_chat_user.php file

   echo '<ul class="nav nav-list">';
        for($i = 0 ; $i < count($list) ; $i++)
        {
            echo '<li>';    
            echo '<div class="user_list">';
                echo '<img src="../images/default_profile_pic/d_boy.png" class="img-rounded">';
                echo '<a href="#" value="'.$list[$i]['c_id'].'" data-toggle="c_id" id="c_id">'.$list[$i]['fn'].' '.$list[$i]['ln'].'</a>';
            echo '</div>';
            echo '</li>';
        }

    echo '</ul>';

I want to get array of values from attribute 'value' of Anchor tag . I am able to get single value in jquery in variable e ... but i want array of values.

2
  • there is no value for a w3.org/TR/html-markup/a.html Commented Oct 11, 2014 at 15:21
  • That doesn't mean he cant get/set an added value attribute. See my answer for an example. Commented Oct 11, 2014 at 15:27

1 Answer 1

1
$.ajax({
        type:'POST',
        url:'../business_logic/send_chat_user.php',       //URL
        data:{ u_id: <?php echo $_SESSION['uid']?>,c_id },  //Data

        beforeSend:function(){},
        success:function(data,status){                      //Data,status
            var arr = [];
            var item = $(data).hide().slideDown("slow");
            var el = $.parseHTML(data);
            var e = $("a", el);
            e.each(function(index) {
                arr.push($(this).attr('value'));
            });

            alert(arr.toString());

            $('#msg').append(item);
        }       
});
Sign up to request clarification or add additional context in comments.

3 Comments

yes similar to this ... i hv printed arr but it shows only the object
You would have to use arr.toString() to print an array's contents or loop through it.
still undefined. actually i wanted to apply it on 'data' which is returned by ajax

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.