如何在JavaScript中找到显示个人资料信息的动态按钮的唯一元素ID。

huangapple go评论63阅读模式
英文:

How do I find the ID of a unique element for a dynamic button that displays profile info in javascript

问题

以下是你要翻译的内容:

I am trying to display a unique user info when the mouse of the user hovers over a button, a unique information page is displayed. This page is a unique div element with a specific ID, which is assigned to the button when the button gets added onto the page dynamically. I am struggling with getting the ID of the info div. Any help would be appreciated. Here is the HTML code where you will be able to see 2 different buttons with 2 different div element named friend_info1 and friend_info2 which are the elements I am trying to get the ID of:

<button type="button" class="friend-button" id="friend-button1" onmouseover=displayInfo(this)>
    <div class="media">
        <img src="default-pic.png" class="mr-3" alt="Default Picture" width="50" height="50">
    </div>
    <div class="media-body">
        <h5 class="mt-0">Friend1</h5>
        <p>status: active</p>
    </div>
</button>
<div class="friend_info" id="friend_info1" onmouseover="getUserInfo(this);">
    <video autoplay muted loop class="backgroundInfo" id="backgroundInfo">
        <source src="Background.mp4" type="video/mp4">
    </video>
    <div class="d-flex">
        <img src="default-pic.png" class="profile-picture" alt="Default Picture" width="200" height="200">
        <div class="d-flex flex-column">
            <div class="p-2">
                <h1 class="Friend_Name">Friend1</h1>
            </div>
            <div class="p-1">
                <h5>status: active</h5>
            </div>
            <div class="p-9">
                <p class="info">Name: Joesdadsadsadsadsa</p>
                <p class="info">Surname: Smithdsadsadsadsadsa</p>
                <p class="info">Phone Number: 07914836605</p>
                <p class="info">Gender: Male</p>
                <p class="info">Date of birth: 14/02/2003</p>
            </div>
        </div>
    </div>
</div>
<button type="button" class="friend-button" id="friend-button2" onmouseover=displayInfo(this)>
    <div class="media">
        <img src="default-pic.png" class="mr-3" alt="Default Picture" width="50" height="50">
    </div>
    <div class="media-body">
        <h5 class="mt-0">Friend1</h5>
        <p>status: active</p>
    </div>
</button>
<div class="friend_info" id="friend_info2" onmouseover=getUserInfo(this)>
    <video autoplay muted loop class="backgroundInfo" id="backgroundInfo">
        <source src="Background.mp4" type="video/mp4">
    </video>
    <div class="d-flex">
        <img src="default-pic.png" class="profile-picture" alt="Default Picture" width="200" height="200">
        <div class="d-flex flex-column">
            <div class="p-2">
                <h1 class="Friend_Name">Friend1</h1>
            </div>
            <div class="p-1">
                <h5>status: active</h5>
            </div>
            <div class="p-9">
                <p class="info">Name: John</p>
                <p class="info">Surname: Smithers</p>
                <p class="info">Phone Number: 07567836505</p>
                <p class="info">Gender: Male</p>
                <p class="info">Date of birth: 04/12/2000</p>
            </div>
        </div>
    </div>
</div>

Here is the javascript for which I need the unique ID for. Remember, I cannot use getElementById as the id changes accordingly to which button I am hovering:

var friend_button = document.getElementById("friend-button");

function displayInfo(button){
  const position = button.getAttribute("data-position");
  var friend_info = document.getElementById(`friend_info${position}`);
  var coords = button.getBoundingClientRect();
  var coordsOfInfo = $('.backgroundInfo').width();
  var subtractWidth = coords.left-coordsOfInfo;
  friend_info.style.left = subtractWidth+"px";
  friend_info.style.top = coords.top + "px";
  friend_info.style.display = "block";
  button.addEventListener("mouseout", function(){
  friend_info.style.display = "none";
});
};

I have tried creating a function that gets the ID of the div element when hovered however I was getting returned a null value evertime I hovered over the element (You can see my attempt in the html code above with the "getUserInfo(this)":

function getUserInfo(information) {
  console.log(information.id);
  return information.id;
}
英文:

I am trying to display a unique user info when the mouse of the user hovers over a button, a unique information page is displayed. This page is a unique div element with a specific ID, which is assigned to the button when the button gets added onto the page dynamically. I am struggling with getting the ID of the info div. Any help would be appreciated. Here is the HTML code where you will be able to see 2 different buttons with 2 different div element named friend_info1 and friend_info2 which are the elements I am trying to get the ID of:

&lt;button type=&quot;button&quot; class=&quot;friend-button&quot; id=&quot;friend-button1&quot; onmouseover=displayInfo(this)&gt;
&lt;div class=&quot;media&quot;&gt;
&lt;img src=&quot;default-pic.png&quot; class=&quot;mr-3&quot; alt=&quot;Default Picture&quot; width=&quot;50&quot; height=&quot;50&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;media-body&quot;&gt;
&lt;h5 class=&quot;mt-0&quot;&gt;Friend1&lt;/h5&gt;
&lt;p&gt;status: active&lt;/p&gt;
&lt;/div&gt;
&lt;/button&gt;
&lt;div class=&quot;friend_info&quot; id=&quot;friend_info1&quot; onmouseover=&quot;getUserInfo(this);&quot;&gt;
&lt;video autoplay muted loop class=&quot;backgroundInfo&quot; id=&quot;backgroundInfo&quot;&gt;
&lt;source src=&quot;Background.mp4&quot; type=&quot;video/mp4&quot;&gt;
&lt;/video&gt;
&lt;div class=&quot;d-flex&quot;&gt;
&lt;img src=&quot;default-pic.png&quot; class=&quot;profile-picture&quot; alt=&quot;Default Picture&quot; width=&quot;200&quot; height=&quot;200&quot;&gt;
&lt;div class=&quot;d-flex flex-column&quot;&gt;
&lt;div class=&quot;p-2&quot;&gt;
&lt;h1 class=&quot;Friend_Name&quot;&gt;Friend1&lt;/h1&gt;
&lt;/div&gt;
&lt;div class=&quot;p-1&quot;&gt;
&lt;h5&gt;status: active&lt;/h5&gt;
&lt;/div&gt;
&lt;div class=&quot;p-9&quot;&gt;
&lt;p class=&quot;info&quot;&gt;Name: Joesdadsadsadsadsa&lt;/p&gt;
&lt;p class=&quot;info&quot;&gt;Surname: Smithdsadsadsadsadsa&lt;/p&gt;
&lt;p class=&quot;info&quot;&gt;Phone Number: 07914836605&lt;/p&gt;
&lt;p class=&quot;info&quot;&gt;Gender: Male&lt;/p&gt;
&lt;p class=&quot;info&quot;&gt;Date of birth: 14/02/2003&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt; 
&lt;button type=&quot;button&quot; class=&quot;friend-button&quot; id=&quot;friend-button2&quot; onmouseover=displayInfo(this)&gt;
&lt;div class=&quot;media&quot;&gt;
&lt;img src=&quot;default-pic.png&quot; class=&quot;mr-3&quot; alt=&quot;Default Picture&quot; width=&quot;50&quot; height=&quot;50&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;media-body&quot;&gt;
&lt;h5 class=&quot;mt-0&quot;&gt;Friend1&lt;/h5&gt;
&lt;p&gt;status: active&lt;/p&gt;
&lt;/div&gt;
&lt;/button&gt;
&lt;div class=&quot;friend_info&quot; id=&quot;friend_info2&quot;  onmouseover=getUserInfo(this)&gt;
&lt;video autoplay muted loop class=&quot;backgroundInfo&quot; id=&quot;backgroundInfo&quot;&gt;
&lt;source src=&quot;Background.mp4&quot; type=&quot;video/mp4&quot;&gt;
&lt;/video&gt;
&lt;div class=&quot;d-flex&quot;&gt;
&lt;img src=&quot;default-pic.png&quot; class=&quot;profile-picture&quot; alt=&quot;Default Picture&quot; width=&quot;200&quot; height=&quot;200&quot;&gt;
&lt;div class=&quot;d-flex flex-column&quot;&gt;
&lt;div class=&quot;p-2&quot;&gt;
&lt;h1 class=&quot;Friend_Name&quot;&gt;Friend1&lt;/h1&gt;
&lt;/div&gt;
&lt;div class=&quot;p-1&quot;&gt;
&lt;h5&gt;status: active&lt;/h5&gt;
&lt;/div&gt;
&lt;div class=&quot;p-9&quot;&gt;
&lt;p class=&quot;info&quot;&gt;Name: John&lt;/p&gt;
&lt;p class=&quot;info&quot;&gt;Surname: Smithers&lt;/p&gt;
&lt;p class=&quot;info&quot;&gt;Phone Number: 07567836505&lt;/p&gt;
&lt;p class=&quot;info&quot;&gt;Gender: Male&lt;/p&gt;
&lt;p class=&quot;info&quot;&gt;Date of birth: 04/12/2000&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

Here is the javascript for which I need the unique ID for. Remember, I cannot use getElementById as the id changes accordingly to which button I am hovering:

var friend_button = document.getElementById(&quot;friend-button&quot;);
function displayInfo(button){
const position = button.getAttribute(&quot;data-position&quot;);
var friend_info = document.getElementById(`friend_info${position}`);
var coords = button.getBoundingClientRect();
var coordsOfInfo = $(&#39;.backgroundInfo&#39;).width();
var subtractWidth = coords.left-coordsOfInfo;
friend_info.style.left = subtractWidth+&quot;px&quot;;
friend_info.style.top = coords.top + &quot;px&quot;;
friend_info.style.display = &quot;block&quot;;
button.addEventListener(&quot;mouseout&quot;, function(){
friend_info.style.display = &quot;none&quot;;
});
};

I have tried creating a function that gets the ID of the div element when hovered however I was getting returned a null value evertime I hovered over the element (You can see my attempt in the html code above with the "getUserInfo(this)".

function getUserInfo(information) {
console.log(information.id);
return information.id;
}

答案1

得分: 1

这是你要翻译的内容:

"I am not sure if this is what you are trying to achieve. There are few ways to do it.

This method is that assuming that unique user info element id are in a fixed format as such friend_infoX where the X is the numbering. If this is the case, then you can add a custom attribute to the button data-position and set the numbering as the value. By this way, you can dynamically set and use the getElementById to access the respective user info.

function displayInfo(button){
    const position = button.getAttribute("data-position");
    var friend_info = document.getElementById(`friend_info${position}`);
    var coords = button.getBoundingClientRect();
    var coordsOfInfo = $('.backgroundInfo').width();
    var subtractWidth = coords.left-coordsOfInfo;
    friend_info.style.left = subtractWidth+"px";
    friend_info.style.top = coords.top + "px";
    friend_info.style.display = "block";
    button.addEventListener("mouseout", function(){
    friend_info.style display = "none";
  });
};
.friend_info{
  display: none
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button type="button" class="friend-button" id="friend-button1" data-position="1" onmouseover=displayInfo(this)>
    <div class="media">
        <img src="default-pic.png" class="mr-3" alt="Default Picture" width="50" height="50">
    </div>
    <div class="media-body">
        <h5 class="mt-0">Friend1</h5>
        <p>status: active</p>
    </div>
</button>
<div class="friend_info" id="friend_info1">
    <video autoplay muted loop class="backgroundInfo" id="backgroundInfo">
        <source src="Background.mp4" type="video/mp4">
    </video>
    <div class="d-flex">
        <img src="default-pic.png" class="profile-picture" alt="Default Picture" width="200" height="200">
        <div class="d-flex flex-column">
            <div class="p-2">
                <h1 class="Friend_Name">Friend1</h1>
            </div>
            <div class="p-1">
                <h5>status: active</h5>
            </div>
            <div class="p-9">
                <p class="info">Name: Joesdadsadsadsadsa</p>
                <p class="info">Surname: Smithdsadsadsadsadsa</p>
                <p class="info">Phone Number: 07914836605</p>
                <p class="info">Gender: Male</p>
                <p class="info">Date of birth: 14/02/2003</p>
            </div>
        </div>
    </div>
</div> 
<button type="button" class="friend-button" id="friend-button2" data-position="2" onmouseover=displayInfo(this)>
    <div class="media">
        <img src="default-pic.png" class="mr-3" alt="Default Picture" width="50" height="50">
    </div>
    <div class="media-body">
        <h5 class="mt-0">Friend2</h5>
        <p>status: active</p>
    </div>
</button>
<div class="friend_info" id="friend_info2">
    <video autoplay muted loop class="backgroundInfo" id="backgroundInfo">
        <source src="Background.mp4" type="video/mp4">
    </video>
    <div class="d-flex">
        <img src="default-pic.png" class="profile-picture" alt="Default Picture" width="200" height="200">
        <div class="d-flex flex-column">
            <div class="p-2">
                <h1 class="Friend_Name">Friend2</h1>
            </div>
            <div class="p-1">
                <h5>status: active</h5>
            </div>
            <div class="p-9">
                <p class="info">Name: John</p>
                <p class="info">Surname: Smithers</p>
                <p class="info">Phone Number: 07567836505</p>
                <p class="info">Gender: Male</p>
                <p class="info">Date of birth: 04/12/2000</p>
            </div>
        </div>
    </div>
</div>

Besides, for the function getUserInfo(this), you can simply get the element id by doing as such:

function getUserInfo(information) {
  //var gettingId = document.getElementById(information); //You dont need this
  console.log(information.id);
  return information.id;
}
英文:

I am not sure if this is what you are trying to achieve. There are few ways to do it.

This method is that assuming that unique user info element id are in a fixed format as such friend_infoX where the X is the numbering. If this is the case, then you can add a custom attribute to the button data-position and set the numbering as the value. By this way, you can dynamically set and use the getElementById to access the respective user info.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function displayInfo(button){
const position = button.getAttribute(&quot;data-position&quot;);
var friend_info = document.getElementById(`friend_info${position}`);
var coords = button.getBoundingClientRect();
var coordsOfInfo = $(&#39;.backgroundInfo&#39;).width();
var subtractWidth = coords.left-coordsOfInfo;
friend_info.style.left = subtractWidth+&quot;px&quot;;
friend_info.style.top = coords.top + &quot;px&quot;;
friend_info.style.display = &quot;block&quot;;
button.addEventListener(&quot;mouseout&quot;, function(){
friend_info.style.display = &quot;none&quot;;
});
};

<!-- language: lang-css -->

.friend_info{
display: none
}

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;button type=&quot;button&quot; class=&quot;friend-button&quot; id=&quot;friend-button1&quot; data-position=&quot;1&quot; onmouseover=displayInfo(this)&gt;
&lt;div class=&quot;media&quot;&gt;
&lt;img src=&quot;default-pic.png&quot; class=&quot;mr-3&quot; alt=&quot;Default Picture&quot; width=&quot;50&quot; height=&quot;50&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;media-body&quot;&gt;
&lt;h5 class=&quot;mt-0&quot;&gt;Friend1&lt;/h5&gt;
&lt;p&gt;status: active&lt;/p&gt;
&lt;/div&gt;
&lt;/button&gt;
&lt;div class=&quot;friend_info&quot; id=&quot;friend_info1&quot;&gt;
&lt;video autoplay muted loop class=&quot;backgroundInfo&quot; id=&quot;backgroundInfo&quot;&gt;
&lt;source src=&quot;Background.mp4&quot; type=&quot;video/mp4&quot;&gt;
&lt;/video&gt;
&lt;div class=&quot;d-flex&quot;&gt;
&lt;img src=&quot;default-pic.png&quot; class=&quot;profile-picture&quot; alt=&quot;Default Picture&quot; width=&quot;200&quot; height=&quot;200&quot;&gt;
&lt;div class=&quot;d-flex flex-column&quot;&gt;
&lt;div class=&quot;p-2&quot;&gt;
&lt;h1 class=&quot;Friend_Name&quot;&gt;Friend1&lt;/h1&gt;
&lt;/div&gt;
&lt;div class=&quot;p-1&quot;&gt;
&lt;h5&gt;status: active&lt;/h5&gt;
&lt;/div&gt;
&lt;div class=&quot;p-9&quot;&gt;
&lt;p class=&quot;info&quot;&gt;Name: Joesdadsadsadsadsa&lt;/p&gt;
&lt;p class=&quot;info&quot;&gt;Surname: Smithdsadsadsadsadsa&lt;/p&gt;
&lt;p class=&quot;info&quot;&gt;Phone Number: 07914836605&lt;/p&gt;
&lt;p class=&quot;info&quot;&gt;Gender: Male&lt;/p&gt;
&lt;p class=&quot;info&quot;&gt;Date of birth: 14/02/2003&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt; 
&lt;button type=&quot;button&quot; class=&quot;friend-button&quot; id=&quot;friend-button2&quot; data-position=&quot;2&quot; onmouseover=displayInfo(this)&gt;
&lt;div class=&quot;media&quot;&gt;
&lt;img src=&quot;default-pic.png&quot; class=&quot;mr-3&quot; alt=&quot;Default Picture&quot; width=&quot;50&quot; height=&quot;50&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;media-body&quot;&gt;
&lt;h5 class=&quot;mt-0&quot;&gt;Friend2&lt;/h5&gt;
&lt;p&gt;status: active&lt;/p&gt;
&lt;/div&gt;
&lt;/button&gt;
&lt;div class=&quot;friend_info&quot; id=&quot;friend_info2&quot;&gt;
&lt;video autoplay muted loop class=&quot;backgroundInfo&quot; id=&quot;backgroundInfo&quot;&gt;
&lt;source src=&quot;Background.mp4&quot; type=&quot;video/mp4&quot;&gt;
&lt;/video&gt;
&lt;div class=&quot;d-flex&quot;&gt;
&lt;img src=&quot;default-pic.png&quot; class=&quot;profile-picture&quot; alt=&quot;Default Picture&quot; width=&quot;200&quot; height=&quot;200&quot;&gt;
&lt;div class=&quot;d-flex flex-column&quot;&gt;
&lt;div class=&quot;p-2&quot;&gt;
&lt;h1 class=&quot;Friend_Name&quot;&gt;Friend2&lt;/h1&gt;
&lt;/div&gt;
&lt;div class=&quot;p-1&quot;&gt;
&lt;h5&gt;status: active&lt;/h5&gt;
&lt;/div&gt;
&lt;div class=&quot;p-9&quot;&gt;
&lt;p class=&quot;info&quot;&gt;Name: John&lt;/p&gt;
&lt;p class=&quot;info&quot;&gt;Surname: Smithers&lt;/p&gt;
&lt;p class=&quot;info&quot;&gt;Phone Number: 07567836505&lt;/p&gt;
&lt;p class=&quot;info&quot;&gt;Gender: Male&lt;/p&gt;
&lt;p class=&quot;info&quot;&gt;Date of birth: 04/12/2000&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

Besides, for the function getUserInfo(this), you can simply get the element id by doing as such:

function getUserInfo(information) {
//var gettingId = document.getElementById(information); //You dont need this
console.log(information.id);
return information.id;
}

huangapple
  • 本文由 发表于 2023年2月24日 00:32:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75547687.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定