Arvind
Arvind

Reputation: 75

Access parent Div id when user press any key on textbox

I want to access data-user-id of the parent div whenever user press any key in the textbox...

class panel is nothing but a chat window contains receiver id which are assigned to him dynamically.

HTML :

<div class="chat-window-container">
            <div class="panel panel-default container" data-user-id="a4c2c119-623e-436d-a750-a521012953fb" id="chat-0001" style="display: block;">
                    <div class="panel-heading" data-toggle="chat-collapse" data-target="#chat-bill">
                        <a href="#" class="close"><i class="fa fa-times"></i></a>
                        <a href="#">
                            <span class="pull-left">
                                <img src="../../Images/bluemanmxxl.png" width="40">
                            </span>
                            <span class="contact-name">ser fdf</span>
                        </a>
                    </div>

                    <div class="panel-body" id="chat-bill" style="height:300px;overflow-y:scroll">
                    <div class="panel-body" id="chat-bill">
                        <div class="media">
                            <div id="chatContainer">
                            </div>
                        </div>
                        <input type="text" id="msg" class="form-control ChatText" onkeydown="return sendfunction(event)" placeholder="Type message...">
                    </div>

                    </div>
            </div>

             <div class="panel panel-default container" data-user-id="7c344592-f323-4baa-869c-a5210129041e" id="chat-0002" style="display: block;">

                    <div class="panel-heading" data-toggle="chat-collapse" data-target="#chat-bill">
                        <a href="#" class="close"><i class="fa fa-times"></i></a>
                        <a href="#">
                            <span class="pull-left">
                                <img src="../../Images/bluemanmxxl.png" width="40">
                            </span>
                            <span class="contact-name">ABC sdsd</span>
                        </a>
                    </div>


                        <div class="media">
                            <div id="chatContainer">
                            </div>
                        </div>
                        <input type="text" id="msg" class="form-control ChatText" onkeydown="return sendfunction(event)" placeholder="Type message...">
            </div>
        </div>

JS :

function sendfunction(e) { 
    var keynum;

    if (window.event) { 
        keynum = e.keyCode; 
    } 
    if (keynum == 13) { 
        strChatText = $("#msg").val(); 
        if (strChatText != '') { 
            var strGroupName = gname; 
            if (typeof strGroupName !== 'undefined' && strGroupName !== false) 
                chat.server.send(strChatText, gname, '@ViewBag.LoginUserId', a); 
            $("#msg").val(''); 
        } 
        return false; 
    } 
}

Upvotes: 0

Views: 197

Answers (2)

Zakaria Acharki
Zakaria Acharki

Reputation: 67515

Working fiddle.

Because id attribute should be unique in same document change the id=msg of inputs to be unique.

The following line should do the work :

$(e.target).parents('.container').attr('data-user-id');

Hpe this helps.

Upvotes: 1

Arvind
Arvind

Reputation: 75

$(e.target).parents('.container').attr('data-user-id')

Upvotes: 0

Related Questions