0

I am calling method onClick function on the img and i want that image id to use further so how to access that image id

  con.innerHTML += '<table id="results" width="915" border="1" style="margin:-46px 0 0 -2px;"><tr><td><input id="chkbox'+j+'" type="checkbox" name="click_1" value="">&nbsp;'
        + str
        + '</td><td colspan="4" align="right"><a href="javascript:presenter.command(\'viewPdf\',{\'path\': \'/images/pdf/animal/Beef/'+j+'.pdf\'});"><img src="images/view.png" style="margin-left:250px;"></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src="images/email.png" id="'+j+'" onClick=mytest()></td></tr>'+ break_line +'<img src="images/line.png" style="margin-bottom:-47px;"></table>';

here is the function i want to store the id of image which is j in this

     function mytest(){

     var ab=document.getElementById('j');

     alert("ali this is wokring fine");

    }
Ali Sajad
  • 59
  • 1
  • 3
  • 10

4 Answers4

3

If you insist on using inline event attributes, just do this:

onClick='mytest(this)'

...which passes a reference to the clicked element into your function:

function mytest(clickedElement) {
    var theId = clickedElement.id;
}

You can then access any of the properties of the element in question. I've shown how to get the id, but you may not need the id, because if you already have a reference to the element you can obviously then work with it directly - given that it's an img, you could, for example, change its src:

clickedElement.src = "someotherimg.jpg";

Note that you don't then need getElementById() at all.

UPDATE: To answer the "what if?" in the comment, you can setup mytest() to receive an id:

function mytest(elId) {
    var ab = document.getElementById(elId);
    // etc
}

And then you can call it both from an onclick handler:

onclick='mytest(this.id);'

...and directly in your code if the id is already known:

mytest('1');
mytest('2');
var someId = '3';
mytest(someId);
nnnnnn
  • 147,572
  • 30
  • 200
  • 241
  • what if i want to call this mytest using id like not onClick becuase i have more than 100 pdf which i want that if id 0 then open 1 and 2 then 2 like this so instead of callin mytest can we call by id simple – Ali Sajad Apr 10 '13 at 09:31
  • I've updated my answer to cover what you seem to be asking in the comment. – nnnnnn Apr 10 '13 at 09:36
  • can you please help me out that how to concate this var of id in pdf name – Ali Sajad Apr 10 '13 at 09:40
  • You'd use the `+` operator, like you already do in the code in your original post. `'/images/pdf/animal/Beef/'+elId+'.pdf\'` - or similar. – nnnnnn Apr 10 '13 at 09:48
  • i am not getting this can you write full code as i added please – Ali Sajad Apr 10 '13 at 09:52
2

What you could do is this:

<img id="some_id" src="images/email.png" onclick="mytest(this)">

function mytest (img) {
  alert(img.id);
}
Erik Nijland
  • 1,181
  • 2
  • 9
  • 24
0

Pass j inside mytest

+ '</td><td colspan="4" align="right"><a href="javascript:presenter.command(\'viewPdf\',{\'path\': \'/images/pdf/animal/Beef/'+j+'.pdf\'});"><img src="images/view.png" style="margin-left:250px;"></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src="images/email.png" id="'+j+'" onClick="mytest('+j+');"></td></tr>'+ break_line +'<img src="images/line.png" style="margin-bottom:-47px;"></table>';

JS:

function mytest(id){

     var ab=document.getElementById(id);

     alert("ali this is wokring fine");

    }
Anoop
  • 23,044
  • 10
  • 62
  • 76
0

In Image tag call the Mytest() function like below

onClick='mytest(this);'

Javascript :

function mytest(imgEle) {
   var ab=imgEle.id;
   alert("ali this is wokring fine");
}
Pandian
  • 8,848
  • 2
  • 23
  • 33