Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send Div id to javascript via onclick

I have a page with several div's. All of the divs have a unique id. I would like to send that id to a javascript function. I have tried to save it as a value as well, and send this.value, but that does not work either.

<div class='recordElem$val' id='$rname' value='$rname'
onclick='javascript:updateRecord(this.value);'>
        $name</div>"
like image 614
richsoni Avatar asked Aug 02 '10 16:08

richsoni


2 Answers

Here is a jQuery answer

  <script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>

  <script type="text/javascript">
   function clicked(item) {
    alert($(item).attr("id"));
   }
  </script>


  <div onclick="clicked(this);" id="test">test</div>
like image 133
Brandon Boone Avatar answered Oct 04 '22 11:10

Brandon Boone


Have a look at two ways to solve this problem

<html>
<body>
    <div id="scopeId" onclick="javascript:testScope.call(this)">Using scope</div>
    <div id="parameterId" onclick="javascript:testId(this.id)">Using Parameter</div>
    <script>
        function testScope(){
            alert("Scope: " + this.id)
        }
        function testId(id){
            alert("Parameter: " + id)
        }
    </script>
</body>
</html>
like image 29
Arun P Johny Avatar answered Oct 04 '22 10:10

Arun P Johny