SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Thread: variable prob

  1. #1
    SitePoint Evangelist Scheisskopf's Avatar
    Join Date
    Nov 2004
    Location
    Southampton, UK
    Posts
    537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    variable prob

    i have the code
    Code:
    function swap_bold(elm, tgt) {
    	tgt = 'bold_' + tgt;
    	if(elm.src.match('text_bold.gif')) {
    		document.forms['text'].tgt.value = '';
    		elm.src='../files/img/fonts/text_bold_shadow.gif';
    	} else {
    		document.forms['text'].d.value = 'b';
    		elm.src='../files/img/fonts/text_bold.gif';
    	}
    }
    tgt makes up a new string to be used in the document.forms line, but instead of using the contents of tgt in the line, javascript tries to look for the elemenet tgt in the page!

    Why is this?

  2. #2
    SitePoint Evangelist Scheisskopf's Avatar
    Join Date
    Nov 2004
    Location
    Southampton, UK
    Posts
    537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    found a solution - use elements[]
    i.e.
    Code:
    document.forms['text'].elements[tgt].value = '';

  3. #3
    SitePoint Guru
    Join Date
    Feb 2005
    Posts
    602
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do this:

    Code:
    function swap_bold(elm, tgt) {
    	tgt = 'bold_' + tgt;
    	if(elm.src.match('text_bold.gif')) {
    		document.forms['text'][tgt]value = '';
    		elm.src='../files/img/fonts/text_bold_shadow.gif';
    	} else {
    		document.forms['text'].d.value = 'b';
    		elm.src='../files/img/fonts/text_bold.gif';
    	}
    }
    In order to access a property with a string, you have to use the syntax object[strProp]. Otherwise, it would be like doing object.'prop' which doesn't make sense (it would have to be object.prop).


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
  •