Results 1 to 6 of 6

Thread: document.getElementById

  1. #1
    Join Date
    Mar 2010


    I have this and it works but it looks very messy, especially if I have over a dozen line of document.getElementById

    Here's my code:

    document.getElementById("a").innerHTML = "Letter a";
    document.getElementById("a").innerHTML = "Letter b";
    document.getElementById("a").innerHTML = "Letter c";
    I can rewrite as
    with (document.getElementById("a")){
       innerHTML = "Letter a";
       innerHTML = "Letter b";
       innerHTML = "Letter c";

    what happen if I have this and the value that I pass in is very random.
    How can I rewrite it so it will look cleaner?

    document.getElementById("a1").innerHTML = "Letter a";
    document.getElementById("a33").innerHTML = "Letter b";
    document.getElementById("cx").innerHTML = "Letter c";
    document.getElementById("dz").innerHTML = "Letter d";
    document.getElementById("x99").innerHTML = "Letter e";
    document.getElementById("fish").innerHTML = "Letter f";

  2. #2
    Join Date
    Mar 2005
    Behind you...
    Typically with things like this you simply want to create a function that returns the element, so you end up with cleaner code. jQuery of course provides this kind of capability, but depending on your project you may not want to include the entire jQuery library only to use a small set of features/functions.

    So the simplest version of what you want would look something like this:
    function _($e) { return document.getElementById($e); }
    _("a1").innerHTML = "Letter a";
    _("a33").innerHTML = "Letter b";
    _("cx").innerHTML = "Letter c";
    _("dz").innerHTML = "Letter d";
    _("x99").innerHTML = "Letter e";
    _("fish").innerHTML = "Letter f";
    But as I keep reading your post I wonder if you are actually asking for a way to reduce the number of lines, not just the size of the code. So instead of having 12+ lines of this same thing, you would like 3 or 4. But if this is the case I would need to know a little bit more about your code as there are several ways this can be achieved, however it will depend on how you have things set up in your code. For instance, you could use a for() loop to go through a list of elements, but this requires you to have those element ids and values you want in an array and I'm not sure if that's possible in your case or not. You could also use the .childNodes method to loop through all of the elements, but this only works if all of the elements are in the same container.
    "Given billions of tries, could a spilled bottle of ink ever fall into the words of Shakespeare?"

  3. #3
    Join Date
    Mar 2010
    what you have is excellent but I was just wondering...
    calling a function for just one line, does that not slow the program down?

  4. #4
    Join Date
    Mar 2005
    Behind you...
    Believe it or not but using the function actually seems to do (slightly) better in performance. (link)

    I'm not really an expert on code performance so forgive me if anything I say is untrue. But based on what I do know, .getElementById() is already a function that exist in the browser's DOM so in essense you are already having to call a function every time you list out document.getElementById("foo"). My guess is this has to do with the javascript being parsed by the browser when the page loads. The browser doesn't run the literal version of the code we see, so it's possible that once all of the code has been parsed, the custom function merely uses up less resources allowing it to run slightly faster that the full DOM call.
    "Given billions of tries, could a spilled bottle of ink ever fall into the words of Shakespeare?"

  5. #5
    Join Date
    Dec 2011
    Centurion, South Africa
    Depending on the actual purpose the code could be written in different ways. For example if you constantly made changes to the elements then having a function, as Sup3rkirby suggested, to quickly query for an element might be the best. However if it's simply for initializing a mass of element values, then you can do it like so:

    (function(o) {var i; for (i in o) {if (o.hasOwnProperty(i)) {document.getElementById(i).innerHTML = o[i];}}}
    	a1   : 'Letter a',
    	a33  : 'Letter b',
    	cx   : 'Letter c',
    	dz   : 'Letter d',
    	x99  : 'Letter e',
    	fish : 'Letter f'
    JavaScript: Learn | Validate | Compact | bionoid

  6. #6
    Join Date
    May 2006
    Somewhere behind your screen
    <!doctype html>
    <meta charset="utf-8" />
    function $(id){return document.getElementById(id);}
    function _(id,content){$(id).innerHTML=content;}
    _('a1','Letter a');
    /* in case that you have many similar id's */
    var i=2;
    while($('a'+i)!==null){_('a'+i,'some content '+i);i++}
    _('a33','Letter b');
    <div id="a1"></div>
    <div id="a2"></div>
    <div id="a3"></div>
    <div id="a4"></div>
    <div id="a5"></div>
    <div id="a6"></div>
    <div id="a7"></div>
    <div id="a8"></div>
    <div id="a9"></div>
    <div id="a10"></div>
    <div id="a33"></div>
    signature under construction

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



X vBulletin 4.2.2 Debug Information

  • Page Generation 0.15444 seconds
  • Memory Usage 2,902KB
  • Queries Executed 15 (?)
More Information
Template Usage (33):
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (6)bbcode_code
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (6)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (6)postbit
  • (6)postbit_onlinestatus
  • (6)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (72):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates