| | |
Help JS Event onclick to get ID
Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Nov 2008
Posts: 16
Reputation:
Solved Threads: 0
im busy with a school project to create a photo viewer type site with java script.
the whole thing i being created from java script and im giving all the img's an ID tag
how do i go about getting the id of the img that i have just clicked.
i know my current code doesn't work properly in IE so help with that would be appreciated as well.
the reason i want the Id is so later i can hopefully use the info to determine which img to "zoom in" etc
the whole thing i being created from java script and im giving all the img's an ID tag
how do i go about getting the id of the img that i have just clicked.
i know my current code doesn't work properly in IE so help with that would be appreciated as well.
the reason i want the Id is so later i can hopefully use the info to determine which img to "zoom in" etc
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
function vulpagina() { alert("vulpagina geactiveerd, jej"); var maindiv=document.getElementById("maindiv"); for(i=1; i<26; i++) { var imgnr = i-1 var div=document.createElement("div"); var img=document.createElement("img"); var br=document.createElement("br"); var txt=document.createTextNode("dit is div " + i); img.src=album[imgnr].url; img.style.margin ="10px 50p"; img.style.padding ="10px"; img.style.height="80%"; img.setAttribute('onclick','getImgId(this)'); img.onclick = getImgId; div.setAttribute("id","divje" + i); div.appendChild(img) div.setAttribute('style','float:left'); div.style.width="20%"; div.style.height="20%"; div.appendChild(txt); maindiv.appendChild(div); //alert("test"); } } function getImgId(obj) { alert('test') imgId = event.srcElement.id alert(imgId) } var album = [ { url: 'images/0006787.jpg', landscape: false, description: 'Shadows to the sea...', photographer: 'Sander', date: new Date('12-8-1999') }, { url: 'images/0004713.jpg', landscape: false }, { url: 'images/0004720.jpg', landscape: false }, { url: 'images/0004731.jpg', landscape: true } ];
•
•
Join Date: Nov 2008
Posts: 16
Reputation:
Solved Threads: 0
hmm is there an edit button?
anyway
i changed my code a bit to using event listeners but i still cant get the Id to appear in the alert i get either HTMLElement or something like that in the alert or i get an empty alert box
anyone know what im doing wrong?.
sorry for double post but i couldnt edit my first one.
anyway
i changed my code a bit to using event listeners but i still cant get the Id to appear in the alert i get either HTMLElement or something like that in the alert or i get an empty alert box
anyone know what im doing wrong?.
sorry for double post but i couldnt edit my first one.
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
function vulpagina() { alert("vulpagina geactiveerd, jej"); var maindiv=document.getElementById("maindiv"); for(i=1; i<26; i++) { var imgnr = i-1 var div=document.createElement("div"); var img=document.createElement("img"); var br=document.createElement("br"); var txt=document.createTextNode("dit is div " + i); img.src=album[imgnr].url; img.style.margin ="10px 50p"; img.style.padding ="10px"; img.style.height="80%"; img.addEventListener('click',getImgId, false); div.setAttribute("id","divje" + i); div.appendChild(img) div.setAttribute('style','float:left'); div.style.width="20%"; div.style.height="20%"; div.appendChild(txt); maindiv.appendChild(div); //alert("test"); } } function getImgId(e) { var target = e.target; if (window.event) e = window.event; var srcElement = e.srcElement? e.srcElement : e.target; alert(srcElement.id) //imgId = event.srcElement.id //alert(imgId) }
Last edited by Sereal_Killer; Sep 12th, 2009 at 7:18 pm.
•
•
•
•
hmm is there an edit button?
anyway
i changed my code a bit to using event listeners but i still cant get the Id to appear in the alert i get either HTMLElement or something like that in the alert or i get an empty alert box
anyone know what im doing wrong?.
sorry for double post but i couldnt edit my first one.
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
function vulpagina() { var maindiv=document.getElementById("maindiv"); for(i=0; i<26; i++){ var imgnr = i; var div=document.createElement("div"); var img=document.createElement("img"); var br=document.createElement("br"); var txt=document.createTextNode("dit is div " + i); img.src=album[imgnr].url; img.style.margin ="10px"; img.style.padding ="10px"; img.style.height="80%"; img.onclick=getImgId; div.setAttribute("id","divje" + i); div.appendChild(img) div.setAttribute('style','float:left'); div.style.width="20%"; div.style.height="20%"; div.appendChild(br); div.appendChild(txt); maindiv.appendChild(div); } } function getImgId(){ id = this.id; alert( id ) }
•
•
•
•
no grade yet. its due friday.
this is a chalange for me with onyl 2 weeks of js lessons lol.
anyway thx for the help.
But you should have told us that the solution:
.
.
.
img.onclick=getImgId;
.
.
.
function getImgId(){ id = this.id; alert( id ) }
.
.
.
works!
![]() |
Similar Threads
- products order in onclick event (PHP)
- onclick image?? (PHP)
- problems editing dataGrid and 'Tab' delimited text file (C#)
- Buttons help!!!! (Pascal and Delphi)
- addEventListener not adding var in function (JavaScript / DHTML / AJAX)
- help needed in javascript (JavaScript / DHTML / AJAX)
- function in php (PHP)
- Problem with Keyboard Event <Enter> Key (JavaScript / DHTML / AJAX)
- event object (HTML and CSS)
- Dreamweaver Help (Site Layout and Usability)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: find text
- Next Thread: run command when paste
| Thread Tools | Search this Thread |
acid2 ajax ajaxcode ajaxexample ajaxhelp ajaxjspservlets animate array automatically beta box browser calendar captchaformproblem cart close codes column css date debugger decimal dependent design dom download dropdown element embed enter error events firefox focus form frameworks getselection google gwt gxt hiddenvalue highlightedword hint html htmlform ie7 iframe image() index java javascript javascripthelp2020 jawascriptruntimeerror jquery jsp libcurl listbox maps masterpage media menu microsoft mimic mp4 onerror onmouseover paypal php player position post problem programming prototype rating redirect regex safari scale scriptlets search security select software sql starrating synchronous text textarea toggle unicode validation variables w3c webservice website window windowofwords windowsxp xml





