JavaScript - Set One Element Width Equal To Another
Hi,
I'd like to set the div width equal to the image width. Here's what I can think of: Code: <script type="text/javascript"> window.onload = function () { document.getElementById('foo').style.width = document.getElementById('bar').width + 'px'; } </script> <div id="foo" style="background:red;"><img id="bar" src="image.jpg"></div> It seems to be working, but I'm not sure if it's correct coding. Thanks in advance for checking the code or any improvement you suggest! Rain lover Similar TutorialsHello, let me try to explain better what I'm trying to do. I'm a real newbie I don't know much javascript but I understand more or less the logic behind it... tell me if this solution should work and if you know how to do it please show me. I have this page. http://felixdeportu.com/5/ The images are inside a div#content div#photo. I need to make div#photo's width to match the total width measurement of all the images it contains. If you load the page and you don't touch the size, it will work fine... but that's not realistic. If I resize the page, which will happen often on this kind of page (I'm assuming), the whole thing goes haywire (try it, scroll to the last image and resize the page you'll see what I mean). What can I do? Is my idea the right solution? Am I not explaining this clearly enough? Let me know please, I'm desperate. I've been trying to get CSS to do this for me for 3 hours now. Nothing works... Hey all, I'm a new Javascript coder; it's still pretty easy for me to get snagged on stuff. Anyways, I had a question for a specific project I've been trying to figure out. I have an element set with CSS to a width of 160px. What I want is a way for users to the site to drag a slider back and forth to set the width of the button. Some sort of smooth animation while this is happening would also be nice. I'd also like a way to set a max width on the element. So, can this be done? I'm guessing it would use jQuery, but I don't really know. Does anyone have any ideas? AA The 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'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! Hi all, I'll get it out their early, I am somewhat of a javascript novice. Here is the code I am working with: Code: <script type="application/javascript"> $(document).ready(function() { var numitems = $("#foo6 > li").length; var secondstartpoint = numitems -3; }); </script> <script type="text/javascript" language="javascript"> $(function() { $("#foo6").carouFredSel({ width : 136, height: 822, scroll : { items:1 }, items : { visible: 6, start: secondstartpoint }, easing:"swing", auto : false, prev : "#next3", next : "#prev4", direction:"down", align:"left" }); }); </script> As you can probably see I am trying to set the start point of a jquery carousel to equal a variable that I have created. You can see I have tried " start: secondstartpoint", but this isn't working. I think possibly because the way the plugin is formatted so that the "start" option can only equal a number. Can anyone shed some light on this issue and perhaps even suggest a solution? Thanks in advance guys Dan Could anyone tell me (or link me to some place that can) why the following if-statement will execute? if (5!=6 || 7!=8) { alert("Why does this work?"); } Thanks This is the program.and here is the script. I have tried all I know without success. This Sources program is on another css/html forum for another issue. Frank Code: //** Dynamic Drive Equal Columns Height script v1.01 (Nov 2nd, 06) //** http://www.dynamicdrive.com/style/blog/entry/css-equal-columns-height-script/ var ddequalcolumns=new Object() //Input IDs (id attr) of columns to equalize. Script will check if each corresponding column actually exists: ddequalcolumns.columnswatch=["Content1", "content2", "content3",] ddequalcolumns.setHeights=function(reset){ var tallest=0 var resetit=(typeof reset=="string")? true : false for (var i=0; i<this.columnswatch.length; i++){ if (document.getElementById(this.columnswatch[i])!=null){ if (resetit) document.getElementById(this.columnswatch[i]).style.height="auto" if (document.getElementById(this.columnswatch[i]).offsetHeight>tallest) tallest=document.getElementById(this.columnswatch[i]).offsetHeight } } if (tallest>0){ for (var i=0; i<this.columnswatch.length; i++){ if (document.getElementById(this.columnswatch[i])!=null) document.getElementById(this.columnswatch[i]).style.height=tallest+"px" } } } ddequalcolumns.resetHeights=function(){ this.setHeights("reset") } ddequalcolumns.dotask=function(target, functionref, tasktype){ //assign a function to execute to an event handler (ie: onunload) var tasktype=(window.addEventListener)? tasktype : "on"+tasktype if (target.addEventListener) target.addEventListener(tasktype, functionref, false) else if (target.attachEvent) target.attachEvent(tasktype, functionref) } ddequalcolumns.dotask(window, function(){ddequalcolumns.setHeights()}, "load") ddequalcolumns.dotask(window, function(){if (typeof ddequalcolumns.timer!="undefined") clearTimeout(ddequalcolumns.timer); ddequalcolumns.timer=setTimeout("ddequalcolumns.resetHeights()", 200)}, "resize") How would I clean up this code? it looks so ugly, would I put it in a for loop and make it loop through and set the rand2, rand3, rand4 = place... there so there is a lot less lines of code(which is what I want) I need a cleaner way to do this, so it isn't so much space. Can someone help? there needs to be different variables because I need the random to be different for each, so rand2 = different random value than rand3. var place = [74,111,148,185,222,259,296,333,370]; var random = [rand,rand2,rand3,rand4,rand5,rand6,rand7,rand8,rand9]; var rand = place[Math.floor(Math.random()*place.length)]; var rand2 = place[Math.floor(Math.random()*place.length)]; var rand3 = place[Math.floor(Math.random()*place.length)]; var rand4 = place[Math.floor(Math.random()*place.length)]; var rand5 = place[Math.floor(Math.random()*place.length)]; var rand6 = place[Math.floor(Math.random()*place.length)]; var rand7 = place[Math.floor(Math.random()*place.length)]; var rand8 = place[Math.floor(Math.random()*place.length)]; var rand9 = place[Math.floor(Math.random()*place.length)]; var newrand = place[Math.floor(Math.random()*place.length)]; var newrand1 = place[Math.floor(Math.random()*place.length)]; var newrand2 = place[Math.floor(Math.random()*place.length)]; var newrand3 = place[Math.floor(Math.random()*place.length)]; var newrand4 = place[Math.floor(Math.random()*place.length)]; var newrand5 = place[Math.floor(Math.random()*place.length)]; var newrand6 = place[Math.floor(Math.random()*place.length)]; var newrand7 = place[Math.floor(Math.random()*place.length)]; var newrand8 = place[Math.floor(Math.random()*place.length)]; var newrand9 = place[Math.floor(Math.random()*place.length)]; var old = place[Math.floor(Math.random()*place.length)]; var old1 = place[Math.floor(Math.random()*place.length)]; var old2 = place[Math.floor(Math.random()*place.length)]; var old3 = place[Math.floor(Math.random()*place.length)]; var old4 = place[Math.floor(Math.random()*place.length)]; var old5 = place[Math.floor(Math.random()*place.length)]; var old6 = place[Math.floor(Math.random()*place.length)]; var old7 = place[Math.floor(Math.random()*place.length)]; var old8 = place[Math.floor(Math.random()*place.length)]; var old9 = place[Math.floor(Math.random()*place.length)]; I'm looking for a simple javascript that can fill in the subject of an email message with whatever is in the title tags of the page they're on and also to include the link the the page in the body. [CODE] <a href="mailto:e@o.com?subject=&body=http://www.o.htm" title="Question"> [CODE] Thanks! I want to set the width of a div to be the same width of the image inside the div. The following code works great. But... The images are different widths, so both wrappers are set to the width of the first image. Without having to add an ID to each wrapper or image, can the wrapper width be set to the image width using the name of the image as the unique identifier? Any help would be great! PHP Code: jQuery: $(document).ready(function(){ var newWidth = $('div.wrapper img').attr('width'); $('div.wrapper').width(newWidth); }); HTML: <div class="wrapper"> <p><img src="image1.jpg" width="200" /><br /> Caption</p> </div> <div class="wrapper"> <p><img src="image2.jpg" width="250" /><br /> Caption</p> </div> Need to set the width of a Table that is being spit out by this god awful CMS... please help me... change the html attribute width! Hi, (new to javascript) Code: digAudio = document.createElement("audio"); digAudio.setAttribute('src',hypeDocument.documentName()+'_Resources/'+'digging.m4a'); digAudio.id = "diggingAudio"; newAudio = document.getElementById("diggingAudio"); //returns null the audio element is added (I can play the audio) but I can't get it by its id ? thank you I have the following code Code: <div class="thdnrml"> <td><a href="somelink">fonfof</a></td> </div> what i want to do is this: Code: <div class="thdnrml"> <td><a href="somelink">fonfof</a><a href="anotherlink">another_link</a></td> </div> how can i go about achieving this? Alright, this is going to sound strange, but bear with me here... Code: <input type="text" name="_F0827U" size="015" maxlength="015"> <input type="submit" class="cmdkey" name="_K040827" value="..."> Say I have multiple isntances that look similar to the above. However, I have no possible way of identifying them uniquely (because I don't know the name prior to generation). Is it posable, using JS, to snag the previous element and strip it of it's tags then rewrite a new element isntead of the two originals, say using an onClick() event on either of the elements? Alright, I need a little help with my javascript. I'm a bit of a novice when it comes to programming so I apologize if this appears trivial. I have an element ID that begins with bb + a randomized number between 1-100,000. This number changes after every action so what I need is a way to retrieve this new value each time and implement it into my code. I can view the number using firebug but I need a way to automate this process. Do any of you have some advice about how I could approach this problem? Im trying to duplicate an image multiple times here is what I have Code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <script type="text/javascript"> var totalNumber = 1000; function insert(num) { var cont = document.getElementById("cont"); var img = document.createElement("img"); var percent = num/totalNumber; var result = cont.offsetWidth * percent; var dup = result/100*2; img.src = "guy.jpg"; cont.appendChild(img); } </script> <style type="text/css"> #cont {height:100px; width:500px;} </style> </head> <body> <div id="cont"></div> </body> <script type="text/javascript"> insert(3000); </script> </html> So Im trying to get img to repeat the variable "dup" so you would see <img src="guy.jpg"> 30 times. I know for sure that lastRowTD_s[i] does not have "textarea" element ! The whole page is html valid. Code: else if (lastRowTD_s[i].getElementsByTagName('textarea')) { alert(lastRowTD_s[i].id); //checked, it is the row that does not have "textarea" element. alert(lastRowTD_s[i].getElementsByTagName('textarea')); // gives me [Object HTML collection] !? alert(lastRowTD_s[i].getElementsByTagName('textarea')[0]); //gives me "undefined", heh where has it gone since previous line ? } I'm trying to add an input element to all forms using appendChild. The script first searches for input elements with the name of "myinput". If it finds an input element with that name, it assigns the value "myvalue". If it can't find an input element with that name, it tries to create a new one for all forms. Code: if (document.getElementsByName('myinput')) { var testcode = document.getElementsByName('myinput'); for (var i=0; i < testcode.length; i++){ testcode[i].value = "myvalue";} } else { var z = document.createElement("input"); z.type = "hidden"; z.name = "myinput"; z.value = "myvalue"; z.id = "mycode"; var d = document.getElementsByTagName('form'); for (var i=0; i < d.length; i++){ d.appendChild(z);} } What's wrong with my code? Thanks! HI I want to write a search for my site .I make a new panel in the left of my menue and it has a text input and an image which when that user click on it javascript opens a new window which is a php page.The part I don't know how to do is when the user click on the image it should get the content of the text input and pass it to the url.For the hyperlink of the image I use "<a href="javascriptpen_window('includes/func/search.php?id=" But I don't know what should I write after that to get the content of the text input and put it in the next of the id. How can I do so? Thanks |