Jump to content

createElement() and getAttribute()


niche

Recommended Posts

Can someone work createElement into this script to demo its use?I think the two can work together, but I don't know how.

<html><head><script type="text/javascript">function myFunction(anid){  var cssatr = document.getElementById(anid).getAttribute('style');   // get the value of "style"  var htmlanid = document.getElementById(anid).innerHTML;        // get html code  return ('css style = '+ cssatr+ '<br>html = '+ htmlanid);}</script></head><body><div id="anid" style="float:right;">Some html code</div><script type="text/javascript">document.write(myFunction('anid'))</script></body></html>

Link to comment
Share on other sites

How close is this to display a div ? Admittedly, I'm guessing at the use of createElement() and setAttribute().

<html>  <head>    <script type="text/javascript">        var div = document.createElement('div');        div.style.display = 'absolute';        div.style.left = '500px';        div.style.top = '200px';        div.style.width = '100px';        div.style.height = '100px';        div.style.backgroundColor = 'red';        div.setAttribute('id', 'link-container');    </script>  </head><body>  <div id="link-container">   <script type="text/javascript">     document.write(div);  </script>  </div></body></html>

Link to comment
Share on other sites

document.write is not the way. Every time I see that d### function I cringe. You need to identify a parent element ("link-container" I guess) and use a DOM method like el.appendChild or el.insertBefore to add it to the DOM.

Link to comment
Share on other sites

I'm working towards a updatable link container for display under an input field in a combo box. justsomeguy gave me some code that i've been tring to understand for a couple of days and so far I have failed. Here's what he sent me. it's supposed to be used on a sample text string: "Bob, Brian, Brittany". I thought I needed to understand CreateElement() and getAttribute(), but maybe I'm approaching this wrong. Here's the script he sent. I sure would like help understanding it or another approach you may have. This comes from the topic at: http://w3schools.invisionzone.com/index.ph...t=0&start=0

var hint_text = 'Some name';var div = document.createElement('div');div.style.display = 'absolute';div.style.left = '500px';div.style.top = '200px';div.style.width = '100px';div.style.height = '100px';div.style.backgroundColor = '#fff';div.setAttribute('id', 'link-container');document.getElementsByTagName('body')[0].appendChild(div);var a = document.createElement('a');a.setAttribute('rel', hint_text);a.onClick = function(){  document.getElementById('hint').setValue(this.getAttribute(rel));  document.getElementsByTagName('body')[0].removeChild(div);  }div.appendChild(a);

Link to comment
Share on other sites

I think one problem would be assigning values that would go with position, to display. For display, you probably want to use block, inline, or inline-block.. If you want to set its position, use the position attribute instead.it seems like it would be easier to make a class for all the styles you want to apply to this element you want to create, and then just use javascript to give it that class name. Do you just need help getting it to work, understanding it, or both?

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...