Jump to content
Stef's Coding Community

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>



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

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>

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>

Link to post
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...