views:

103

answers:

4

Hello,

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>"
A: 

How about passing this.id to the javascript function?

Henk
I tried this originally, and for some reason it did not work. Now it works perfectly.
sk8brder343
+1  A: 

Use onclick="updateRecord(this)" and handle it like this:

function updateRecord(div) {
  // statements
  var id = div.id; // or however you want to use the id
  // statements
}
Robusto
+2  A: 

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>
Arun P Johny
A: 

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>
Brandon Boone