markvs Posted October 16, 2012 Report Share Posted October 16, 2012 I'll include some code that will hopefully be self explanatory. The CSS is included in the markup, but I need to learn Javascript. Thanks in advance, Mark <!DOCTYPE html> <html> <head> <style type="text/css"> body {font-size:large; font-family:sans-serif;} a {color:red; font-weight:bold; text-decoration:none; } #content { width: 500px; margin: 100px auto; padding: 20px; background-color: #FFF; border: solid 2px #000; } #content_to700js { width: 700px; margin: 100px auto; padding: 20px; background-color: #000099; color: #FFF; border: solid 2px #000; } </style> </head> <body> <div id="content"> <p><a id="the_link" href="url">Click on Link </a> ...and an iframe will load here, but it will need more width then 500px. So, I need to increase the width of this area on the click of "the_link" I want to keep my HTML, CSS, and Javascript files separate. Can you please explain how to do this. I've created a style block in the CSS for an id called (content_to700js) that I want to switch to on the click of "the_link".</p> <p>Thanks!</p> </div> </body> </html> Quote Link to comment Share on other sites More sharing options...
falkencreative Posted October 17, 2012 Report Share Posted October 17, 2012 I'm not exactly sure what you are asking -- or more specifically, why you need what you are trying to do. But maybe this might help? http://uk.answers.yahoo.com/question/index?qid=20110518140500AAiQ6i9 Quote Link to comment Share on other sites More sharing options...
Dhanishta Posted October 17, 2012 Report Share Posted October 17, 2012 If you want to learn javascript you can go through w3schools site. Quote Link to comment Share on other sites More sharing options...
Mick Posted October 17, 2012 Report Share Posted October 17, 2012 (regarding the post above this one:) Why go to w3schools to learn Javascript? The instruction at Killersites is superb, practical, and engaging. It's hands-on learning by doing. Quote Link to comment Share on other sites More sharing options...
khanahk Posted October 17, 2012 Report Share Posted October 17, 2012 (edited) The below is jQuery, a library that allows for easier JavaScripting... if you include a <script type="text/javascript" src="your_jquery_file.js></script> after downloading the jQuery library (jquery.com) and saving it as your_jquery_file.js, you can use: <script type="text/javascript"> $('#the_link').click(function() // assigns an 'on click' event to the <a> with id 'the_link' { $(this).id = 'content_to700js'; // changes that id to 'content_to700js' }); </script> Edited October 17, 2012 by khanahk Quote Link to comment Share on other sites More sharing options...
markvs Posted October 18, 2012 Author Report Share Posted October 18, 2012 Thanks for the replies. I'm just trying to learn Javascript for now, but later on I would like to learn how to use jQuery. w3s... is helpful at times, but I enjoy the back and forth here on Killersites with explanations to include best practices, etc.. In my code example, forget the iframe or why, I just want to learn how to grab an id using some Javascript, and change it to something else so that it will be styled according to the CSS file. Thanks, Mark Quote Link to comment Share on other sites More sharing options...
falkencreative Posted October 18, 2012 Report Share Posted October 18, 2012 Here's a super simple example with Javascript: <script type="text/javascript"> function testClick() { document.getElementById('test1').id = 'test2'; // finds element with an id of "test1", replaces id with "test2" return false; } </script> <a id="test1" onclick="return testClick()" href="#">Test</a> Quote Link to comment Share on other sites More sharing options...
markvs Posted October 18, 2012 Author Report Share Posted October 18, 2012 Yes Ben that's what I'm looking for, let me try and wrap my brain around it and I'll reply after lunch. Thanks! Quote Link to comment Share on other sites More sharing options...
markvs Posted October 18, 2012 Author Report Share Posted October 18, 2012 Okay, that help a lot. Here is my re-worked code. I'll have to modify things a bit for my application, but the getElementById() is what I needed. The HTML, CSS, and Javascript are all in this file, so you can run it in your browser with ease. Thanks again! <!DOCTYPE html> <html> <head> <style type="text/css"> body {font-size:large; font-family:sans-serif;} a {color:red; font-weight:bold; text-decoration:none; } #content { width: 500px; margin: 100px auto; padding: 20px; background-color: #FFF; border: solid 2px #000; } #content_to700js { width: 700px; margin: 100px auto; padding: 20px; background-color: #000099; color: #FFF; border: solid 2px #000; } </style> </head> <body> <div id="content"> <p><a id="the_link" href="#" onclick="return divWidthTo700()" >Click on Link </a> ...and an iframe will load here, but it will need more width then 500px. So, I need to increase the width of this area on the click of "the_link" I've created a style block in the CSS for an id called (content_to700js) that will come into play with the click of "the_link".</p> <p>Thanks!</p> </div> </body> <script type="text/javascript"> function divWidthTo700() { document.getElementById("content").id = 'content_to700js'; } </script> </html> Quote Link to comment Share on other sites More sharing options...
falkencreative Posted October 18, 2012 Report Share Posted October 18, 2012 Glad you got things working. One thing to note -- the "return false;" at the end of my function was there for a reason. If you try your example and click on the link, note how the "#" appears in the URL bar in the browser? Return false prevents the default action from occurring. Quote Link to comment Share on other sites More sharing options...
markvs Posted October 18, 2012 Author Report Share Posted October 18, 2012 return false; good to know and it's important for this application. Thanks for the heads up. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.