Jump to content
Killersites Community
markvs

change id value in HTML using Javascript

Recommended Posts

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>



Share this post


Link to post
Share on other sites

(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.

Share this post


Link to post
Share on other sites

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 by khanahk

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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>

Share this post


Link to post
Share on other sites

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>

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×