PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, October 15, 2022

[FIXED] How do I get the ID of an Element

 October 15, 2022     dom, dom-events, javascript     No comments   

Issue

<div id="block_msg" style="width:250px;border: solid 1px red;margin: 15px">

<div id="text_msg1">111текст111</div>

<input type="submit" onclick="clickon(**<<<HERE I WANT SEE DIV ID TEXT_MSG1>>>**)" value="Добавить">

</div>

How can I get id="text_msg1" and put it into onclick? How?


Solution

this is the current object, you can than look at the parent and find the divs. This solution is hacky since changing the layout means it will break.

onclick="clickon(this)"

and

function clickon(btn) {
    var wrappingDiv = btn.parentNode;
    var childrenDivs = wrappingDiv.getElementsByTagName("div");
    alert(childrenDivs[0].innerHTML);
}

JSFiddle

It would be better to have a class on the div and look for that class.



Answered By - epascarello
Answer Checked By - Terry (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing