2

I want to sen two id's when onclick occurs like now when div 2 is clicked I want to pass just the id of the 2nd div too & then write script for the 2nd div. is it possible? if so, then how? any example?

<div id="1st_div"></div>
<div id="2nd_div">
     <div id="inner_of_2nd_div" onclick="myfunction('1st_div')"></div>
</div>
michelem
  • 14,430
  • 5
  • 50
  • 66
Debabrata Roy
  • 31
  • 1
  • 1
  • 5

3 Answers3

1

You can pass as many parameters as you wish. Just define the function accordingly.

For instance, for a 2 parameters function myFunction, your onclick should look like

onclick="myfunction(p1, p2)"

and your function myFunction like

myfunction(p1, p2) {
  //do your stuff

}
Veverke
  • 9,208
  • 4
  • 51
  • 95
0

I understand that you want a function which has id argument of the clicked div. If right, use JQuery and do that:

HTML:

<script src="http://code.jquery.com/jquery-1.11.3.min.js"/> 
<div id="1st_div" class="clickable"></div>
                <div id="2nd_div" class="clickable">
                     <div id="inner_of_2nd_div" ></div>
                </div>

JS Jquery:

$(".clickable").click(function(){
        var _id=$(this).attr("id");
        alert("I am div id="+_id+" and I've been clicked right now!");
    });
3pic
  • 1,188
  • 8
  • 26
  • i want to call like this is it possible? because i've a function which works with the 1st_div id. now in the same function call 'onclick' i want call the function, & pass just the id of the div where it is called.
    – Debabrata Roy Jul 29 '15 at 12:15
  • @DebabrataRoy sorry, i dont understand, can you say what you aim, not about code, but about rendering ? – 3pic Jul 29 '15 at 12:28
  • What I understand: A function works with #1st_div id. Ok. It is in 'onclick'. and you want to pass argument, from onclick, for this function. This argument is the ID of the clicked div. Is this it ? See my new answer please. (editted) – 3pic Jul 29 '15 at 12:31
  • onclick iwant to call a function "myfunction('div1') and beside the function want to pass the id of the clicked div. – Debabrata Roy Jul 30 '15 at 16:54
0
        <div id="1st_div"></div>
<div id="2nd_div">
     <div id="inner_of_2nd_div" onclick="myfunction(document.getElementById('1st_div'));">asa</div>

<script>
function myfunction(obj)
{
//here i caught the object 
obj.innerHTML=obj.id;
}
</script>