JavaScript - Reference To Event Target Element Parent
Hello;
I have a situation where an image tag is nested in an anchor tag set. The anchor is assigned an id and is assigned a click/onclick event listener. The anchor id corresponds to a list of objects in an array. In practice, the image tag is receiving the event. The code does setAttribute in another elements img tag src attribute. The image tag receiving the click event, (and passing it to the anchor tag) is event.target. (or event.srcElement, in the case of I.E.) My Question: What I want to know is how to reference the event.target parent element, the anchor tag elements Id attribute. Code: sample anchor/img tag set: <a style="font-family:Arial, Helvetica, sans-serif;color:#cccccc" id="An 1" alt="An 1" href="javascript:"><img style="border:1px solid;border-color:#cccccc" src="WC_port/windows/img//an_1.jpg" width="100" height="131" /><br /><br />An 1</a> I see the double slash in the img src path attribute. It is working as is in test browser: Firefox ( so far) This is in development stages. Later, I will assign the elements a css class rule. in my code, event.target.src has a usable value (src attribute of the img tag) It does not have a usable value for event.target.id (id attribute of the anchor tag) I am doing it this way so in the absence of javascript, the anchors href attribute defaults to a non scripted value. Thank you for time and attention JK Similar TutorialsThe bit of code in bold in the code below is giving me this error in IE: Error: Code: Webpage error details User Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; Trident/4.0; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.5.30729; Tablet PC 2.0; InfoPath.2; OfficeLiveConnector.1.4; .NET CLR 3.0.30729; OfficeLivePatch.1.3; MSN OptimizedIE8;ENGB) Timestamp: Tue, 16 Mar 2010 15:07:11 UTC Message: HTML Parsing Error: Unable to modify the parent container element before the child element is closed (KB927917) Line: 0 Char: 0 Code: 0 URI: http://www.mateinastate.co.uk/users/mateinastate Code: Code: if(document.getElementById('msn1').innerHTML=="") { document.getElementById('msn1').style.display='none'; } if(document.getElementById('yahoo1').innerHTML=="") { document.getElementById('yahoo1').style.display='none'; } if(document.getElementById('skype1').innerHTML=="") { document.getElementById('skype1').style.display='none'; } if(document.getElementById('facebook1').innerHTML.toLowerCase().substr(0,18)=='<a href="http://">') { document.getElementById('facebook1').style.display='none'; } else if(document.getElementById('facebook1').innerHTML.toLowerCase().substr(0,11)=='<a href="">') { document.getElementById('facebook1').style.display='none'; } else { document.getElementById('fbook-add').innerHTML='Facebook Profile'; } What it's saying isn't actually true (I don't think)... this is how the section is laid out: Code: <div id="submenu1" class="anylinkcss"> <ul> <li class="contact-pm"><a href="/index.php?do=pm&act=new&to=$RateViewProfileUserName$&returnurl=$ReturnURL$">PM me</a></li> <li class="contact-email"><a href="/index.php?do=email&id=$RateViewProfileUserId$">E-mail me</a></li> <li class="contact-msn" id="msn1">$RateViewProfileUser-profile_msn$</li> <li class="contact-yahoo" id="yahoo1">$RateViewProfileUser-profile_yahoo$</li> <li class="contact-skype" id="skype1">$RateViewProfileUser-profile_skype$</li> <li class="contact-facebook" id="facebook1"><a href="$RateViewProfileUser-profile_facebook$"><span id="fbook-add"></span></a></li> </ul> </div> <script type="text/javascript" src="/html_1/js/contact-information.js"></script> Does anyone know why this might error in just IE? Hi, I am looking for the equivalent for the following Internet Explorer code for Firefox: Code: z=event.srcElement; z.style.pixelLeft = 500px; Must be something like: Code: function move(e) { z=e.target; z.style.left ..... maybe. This is to assign a new left position to an Element. Any hints welcome ?! Thank you for your help ! I have a parent div, that when hovered over changes the background color. There is a onclick event so that a user can click anywhere within the div for the link to work. Inside that div I have another div with unique links. When I click on any of those links, it also activates the other onclick event. I don't want this. :-/ Here is my code Code: <div class="cat1" onmouseover="style.backgroundColor='#09F'; this.style.cursor='pointer';" onmouseout="style.backgroundColor='';" OnMouseUp="location.href='http://a.com/details.php?id=305366';"> <div class="itemwrapper"> <div class="datebox">Wednesday - January 3rd, 2007</div> <div class="timebox">08:00am - 08:30am</div> <div class="subbox">Blah</div> <div class="peoplebox"><a href="mailto:a@aol.com">George</a></div></div> </div> I'm guessing I need to create some type of function that will detect where the click came from and then decide whether to fire it or not? Thank you in advance for your help. Hi, I have a small problem with a page I'm working on. I have an anchor tag inside a table that has a mouseover event that updates a contextual help box. I need to add a similar mouseover event to the anchor tag that does the same thing, but updates the help box with different text. The problem I have is that when the mouse is over the table, no matter if I move the mouse over the anchor, the help text only displays the message that is passed from the table's mouseover function call. I think the solution *may* be something to do with event bubbling, but I'm not exactly sure how to implement it. The code I'm dealing with is below: The code below is PHP. The table mouseover (for reference): Code: echo "<table class='cleanTable' width='100%' style='margin-top: 5px'"; echo " onMouseOver=\"updateHelpText('Message 1.');\" onMouseOut='clearHelpText();'>"; echo "<tr><th> </th><th> </th><th>First Name</th><th>Surname</th><th>Last Login</th></tr>"; The anchor mouseover: Code: echo "<tr class='cleanrow'><td><a href='newMember.php'><img border='0' src='images/icon_add.gif'></a></td><td></td>"; echo "<td colspan='3'><a class='workbenchLink' href='newMember.php' onMouseOver=\"updateHelpText('Message 2');\" onMouseOut='clearHelpText();'>Invite a new colleague...</a></td></tr>"; echo "</table>"; The function (javascript): Code: function updateHelpText(newText) { document.getElementById("helpBox").innerHTML = "<h4> " + newText + "</h4>"; } Any help with this would be great, thanks. Hi Experts, i have 2 JS array and i need to have dependency between them knowing that 1st Array has parent element of the 2nd one. i.e ARRAY1 ID VAL ID2 2 Dep1 10 3 Dep2 20 4 Dep3 30 ARRAY2 ID VAL ID2 PARENT 20 team1 11 10 21 team2 12 10 22 team3 13 20 23 team4 14 30 i need to have two different DDL's where when i choose VAL = Dep1 on ARRAY1 , i should see on the 2nd DDL only elements with parent corrsponding to Dep1 ( PARENT =10). how this can be possible thanks ok so here is the problem in more detail i have a dropdown menu wrapped in a div attached to this div is the onmouseout event which hides the div this is working the problem however is when moving from 1 link to the next in the dropdown the onmouseout event of the wrapper div is firing and hiding the div prematurely see it live relevant code: Code: <div id="roster_drop" class="drop_menu" style="left: 128px;" onmouseout="this.style.display = 'none';"> <a href="roster.php">Superstars</a> <a href="champions.php">Champions</a> <a href="titlehistory.php">Title History</a> </div> how do i make the event only trigger when your mouse leaves the div itself and not crossing onto a link Hello, I have this short script he Code: function resizeImages() { //Function will be ran onload in the <body> tag var img = document.images; //Puts all the images in an array for (var i = 0; i < img.length; i++) { //A loop to execute a set of functions for every image var parentElement = img[i].parentNode; //Gets parent element of image var parentWidth = parentElement.width(); //Gets width of parent element (value is an integer without "px") var imgWidth = img[i].width(); //Gets image width var parentHeight = parentElement.height(); //Gets height of parent element var imgHeight = img[i].height();//Gets image height if (parentWidth < imgWidth){ img[i].width(parentWidth); //Sets width of image to width of parent element img[i].style.height = "auto"; //Scales height } if (parentHeight > imgHeight){ img[i].height(parentHeight); //Sets height of image to height of parent element img[i].style.width = "auto"; //Scales width } alert(parentWidth); //To test if parentNode is working correctly. Should return an integer. } } It's got some JQuery in it (.width() and .height()) to make manipulation of the dimensions easier to manage. Basically, it's supposed to resize all the images on the webpage to the size of their parent elements (divs) without losing aspect ratio. Image overflow will be hidden so that no matter what the aspect ratio of the image and it's container the largest possible area of the image will be exposed. The alert() function I have at the bottom is supposed to run as a test that each loop is working correctly. However, my images don't get resized and I get no alert. I think the loop may be breaking out before it goes through everything. Anyone have ideas? I'm not a Javascript coder. Thank you, Jared Hello friends, I want to trigger click event on an iframe's element through the main window, Doing it both ways, with or without jquery, will be usable for me. Basically, what I want to do is, I have developed a website http://www.ipjugaad.com, and on every document download page, I have placed an fb like button, what I want to do is that the like button should be automatically clicked when a user click on download button on my website. Give me some idea about how to do it? Hi forum, I am trying to attach an event to a dynamically produced button, and then use stopPropagation and preventDefault. Code: function chapter12_nodeOne() { //create an element var element = document.createElement('input'); //set some attributes element.setAttribute('type', 'button'); element.setAttribute('value', 'submit'); element.setAttribute('id', 'myBtn'); //appendd the element into a DIV document.getElementById('myDiv').appendChild(element); //uses EventUtil to attach an event listener EventUtil.addHandler(element, 'click', function() { alert('event attached'); }); var flag = confirm('prevent default behavior of button?'); if (flag) { var el = document.getElementById('myBtn');/////////////////////////(1) var ev = el.onclick; } } var EventUtil = { addHandler: function(element, type, handler) { //check if the element and the browser support DOM Level 2 event attachment //if the user is not browsing with IE if (element.addEventListener) { element.addEventListener(type, handler, false); } //if user is browsing with IE else if (element.attachEvent) { element.attachEvent("on" + type, handler); } //if user is using a browser that only supports DOM Level 0 event attachment else { element["on" + type] = handler; } }, removeHandler: function(element, type, handler) { //check if the element and the browser support DOM Level 2 event attachment //if the user is not browsing with IE if (element.removeEventListener) { element.removeEventListener(type, handler, false); } //if user is browsing with IE else if (element.detachEvent) { element.detachEvent("on" + type, handler); } //if user is using a browser that only supports DOM Level 0 event attachment else { element["on" + type] = null; } } }; But when debugging I see under el on the line marked with (1) that the onclick event is null. What am I doing wrong?! PS:the event is attached, when I click on the button I get an alert message Hello all, I have a page which has a form and also one iframe in the same. there is a button on the parent form. when the button is clicked, i am submitting the iframe and parent both. forms are getting submitted. but when i do print_r for iframe values, it is blank below is the code Parent page: Code: <? print "<pre>"; print_r($_POST); print "</pre>"; ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE> New Document </TITLE> <META NAME="Generator" CONTENT="EditPlus"> <META NAME="Author" CONTENT=""> <META NAME="Keywords" CONTENT=""> <META NAME="Description" CONTENT=""> </HEAD> <SCRIPT LANGUAGE="JavaScript"> <!-- function validate(){ document.getElementById('mainform').submit(); window.frames['iframe1'].document.forms[0].submit(); } //--> </SCRIPT> <BODY> <FORM METHOD=POST ACTION="" name='mainform' id='mainform'> <TABLE> <TR> <TD>Name</TD> <TD><INPUT TYPE="text" NAME="Name_VC"></TD> </TR> <TR> <TD>Address</TD> <TD><INPUT TYPE="text" NAME="Address_VC"></TD> </TR> <TR> <TD colspan=2><INPUT TYPE="button" name="proceed" value="Save" onClick="validate();"></TD> </TR> </TABLE> </FORM> <iframe src="test1.php" id='iframe1'></iframe> </BODY> </HTML> iframe page : test1.php Code: <? print "<pre>"; print_r($POST); print "</pre>"; ?> <form name='mainform2' id='mainform2' method='post' action=''> Roll number : <INPUT TYPE="text" NAME="Rollnum" value=''> Age: <INPUT TYPE="text" NAME="Age_IN" value=''> </form> Please tell me what is my mistake or how can i achieve values of all 4 fields Million thanks is it possible to capture the control.event or element.event that was fired to invoke the onbeforeunload event. for example, if a button is clicked and it causes the onbeforeunload event to fire can i determine which button was clicked. thanks I have a ondrag event handler and in that I am trying to retrieve e.ClientX but it always return 0 in Mozilla. Works fine in IE though. How can retrieve the clientX and clientY in ondrag event? Hi, I'm relativly new to JS and brand new to the forum so you might need to dumb down your replys for my slightly lacking knowledge. That being said I do have a very solid grasp of html, css and am getting there with JS and its various frameworks. I'm integrating wordpress into an existing site for a friend and currently have the main blog page appear in a DIV. This is the best way to integrate in this case due to many reasons mostly of way the site is constructed. Code: <div class="scroll-pane" id="scrollbox"> WORDPRESS BLOG </div> My issue is that links within that DIV, in the blog, when clicked redirect the page. The simple answer to this would be to have them just open in a new page, which I can easily do with the below code. Code: function Init() { // Grab the appropriate div theDiv = document.getElementById('scrollbox'); // Grab all of the links inside the div links = theDiv.getElementsByTagName('a'); // Loop through those links and attach the target attribute for (var i=0, len=links.length; i < len; i++) { // the _blank will make the link open in new window links[i].setAttribute('target', '_blank'); } } window.onload = Init; But what I'd rather it do is have any link clicked inside the DIV to reload in that same DIV, similar to an iframe, but obviously without using an iframe, due to it's compatibility issues. Is this possible by editing the above code? If not what do I need? Thanks in advance for any help! Hello everyone, I have a page. In that page is an iframe, lets name it 'A'. inside A there is another iframe 'B'. there is a link on that page which on click opens a popup. In that pop up, there is actually a document upload facility. Now i want that after every upload (onSubmit), iframe A should refresh. have done it many times but today its not working. Code: function validate() { (window.parent.opener.location.href)=(window.parent.opener.location.href); window.parent.opener.location.reload(true);// this is tried as well } Can some one please help. it always refreshes only B. Thanks Hi Experimenting. Trying to add a simple onclick event via onload, but I can't get it right? This works: Code: function checkStatus(box){ if(box.checked != box.defaultChecked){ alert("box status has changed"); } } HTML <input type="checkbox" name="mycheckbox" id="mycheckbox" value="&cost=free" onclick="checkStatus(this)" /> This does not: Code: <input type="checkbox" name="mycheckbox" id="mycheckbox" value="&cost=free" /> window.onload = function (){ var mybox = document.getElementById('mycheckbox'); mybox.onclick = checkStatus ; //how to get this to work?? //none of the following work //mybox.onclick = checkStatus; //this calls the function but I don't have a box ref //mybox.onclick = checkStatus(); //mybox.onclick = checkStatus(this); //mybox.onclick = checkStatus(mybox); } LT hey I want to get the href via attributes of this a tag & then this href to be set to a variable I'm working on making a searchable javascript and DOM reference...A chm like what php has. I mainly need it for myself because I don't always have access to the net. I was wondering if anyone would like to contribute? Hi everyone, Does anybody know where I can download a decent Javascript reference in eBook format ?? Hey, I was having some trouble with this, and couldn't seem to find anything similar anywhere. (I'm not sure it's possible). (I know- for a normal variable there are no pointers in JS.) Normally, you can get an object reference: Code: obj.a = {...}; var pointer = obj.a; //now, changes to pointer cause changes to obj.a However, I would really like to be able to get a reference to a "variable" defined with a setter: Code: obj.__defineSetter__("a",function(input){...}); var pointer = obj.a; //but now: pointer = "..."; //does absolutely nothing! (as expected) Anyone know a way to do this? (without using __defineSetter__ to create pointer). If you are wondering, this is what I would like to be able to do: Code: obj = { a:function(value) { obj.other=value; this.__defineSetter__("a",function(input){ //do something with input, depending on obj.other //reset obj.other //reset obj.a to its original definition }); return ? //return pointer to obj.a } } obj.a("...") = OtherFunctionOutput(); obj.a("...2") = OtherFunctionOutput2(); //etc... (This would be extremely useful in the context I'm working with. Basically, obj.a would be oscillating between a function that modifies some obj properties and a variable that can be set using cleaner syntax than is otherwise possible.) (The simple alternative would look like: ) Code: obj.a("...",OtherFunctionOutput()); (But, I'd really like to get the syntax described above working, if possible) Thanks for any help anyone! Hi! I'm working on a JSP based project. On myJsp1.jsp, I open a new window using window.open and store reference in a variable myWin like myWin = window.open(.........); from here i submit the form to myJsp2.jsp. In this file, I want the reference myWin to close the opened window. What I'm doing while submitting form is: myWin = window.open(.............); document.onlinePayStatusSummary.action ="myJsp2.jsp?action=forwardOnlinePayRequest&childWindow=" +myWin; document.onlinePayStatusSummary.submit(); I've read that this way JavaScript references can be passed in url. Now when I do getParameter in myJsp2.jsp as var winRef = "<%=request.getParameter("childWindow").toString()%>"; winRef.close(); I get JavaScript error that this winRef do not support the close() property. Any help? |