SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Evangelist Maujor's Avatar
    Join Date
    Mar 2005
    Location
    Rio de Janeiro - Brazil - South America
    Posts
    409
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    createElement("variable")

    Suppose a tag name retrieved into a variable, something like:
    var tag = form.input.tag.value
    Is there a sintax, or a way to create a var tag element?
    Something like: var currentElement = document.createElement("here goes var tag");
    ----------------------------------------------------------------------
    I've figured out that if I use:
    var currentElement = document.createElement(form.input.tag.value);
    it does work in Firefox but fails in IE.
    So need a more robust solution.
    Mauricio Samy Silva
    http://www.maujor.com/

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    What's the problem? This test piece seems to work fine.

    Code html4strict:
    <form name="form">
        <input type="text" name="input" value="p">
    </form>
    <script>
    var currentElement = document.createElement(document.form.input.value);
    alert(currentElement);
    </script>
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Evangelist Maujor's Avatar
    Join Date
    Mar 2005
    Location
    Rio de Janeiro - Brazil - South America
    Posts
    409
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Very sorry my fault!
    var tag comes from a select form control not a input.
    var tag = form.select.tag.value
    Mauricio Samy Silva
    http://www.maujor.com/

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    The technique still works perfectly fine.

    Code html4strict:
    <form name="form">
        <select name="select">
    		<option value="">Please select an element</option>
    		<option value="h1">Heading level 1</option>
    		<option value="h2">Heading level 2</option>
    		<option value="p">Paragraph</option>
    	</select>
     
    </form>
    <script>
    document.form.select.onchange = createElement;
    function createElement() {
    	var currentElement = document.createElement(this.value);
    	alert(currentElement.nodeName);
    }
    </script>

    The question now becomes, what is going wrong with your code.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Evangelist Maujor's Avatar
    Join Date
    Mar 2005
    Location
    Rio de Janeiro - Brazil - South America
    Posts
    409
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Solved

    Great Paul.
    Your new code help me to solve the problem.
    Many thanks.
    Mauricio Samy Silva
    http://www.maujor.com/

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    I'm pleased to have helped.

    Would you mind informing us about what the trouble was and how you resolved it?
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  7. #7
    SitePoint Evangelist Maujor's Avatar
    Join Date
    Mar 2005
    Location
    Rio de Janeiro - Brazil - South America
    Posts
    409
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sure!
    I was using the tag name as content for the option element instead of the value attribute inside it.
    Code HTML4Strict:
    <option>h1</option>
    instead of
    Code HTML4Strict:
    <option value="h1">h1</option>
    This (wrong) approach works fine in Firefox but fails in IE.

    The work is going here: http://www.maujor.com/temp/forum.html
    Comments are welcome!
    Last edited by Maujor; Apr 28, 2008 at 19:58. Reason: added a link for work
    Mauricio Samy Silva
    http://www.maujor.com/

  8. #8
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Thanks Mauricio for explaining that. This will help others who come across a similar problem.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •