Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    MRW
    MRW is offline
    New to the CF scene
    Join Date
    Aug 2014
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Need help with variable displaying as undefined after running it through a function

    I have a variable called 'a' and I run it through a function. The function pretty much replaces some words. My problem is that when I try to print out a console.log, I receive an output of undefined. Any help will be appreciated. Thanks.

    Code:
    var a = myFunction('divNameGoesHere');
    
    function editDivName('divName') {
    	//Do stuff to div name
    	return(revisedDivName);
    };
    
    console.log(a); //outputs undefined

  • #2
    MRW
    MRW is offline
    New to the CF scene
    Join Date
    Aug 2014
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Correction, it should be the following.

    Code:
    var a = editDivName('divNameGoesHere');
    
    function editDivName('divName') {
    	//Do stuff to div name
    	return(revisedDivName);
    };
    
    console.log(a); //outputs undefined

  • #3
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    3,020
    Thanks
    56
    Thanked 566 Times in 563 Posts
    it would help if you were to show us some real code, or even just a stripped down version of the problem.

    This for example:
    Code:
    return(revisedDivName);
    should be
    Code:
    return revisedDivName;
    the parameter divName is a string, which you probably don't want it to be

    but like I say - hard to say what the issue is, really, without seeing the real code

  • Users who have thanked xelawho for this post:

    MRW (08-10-2014)

  • #4
    MRW
    MRW is offline
    New to the CF scene
    Join Date
    Aug 2014
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks for the reply. The below is the full code. Essentially, I am trying to use the input color attribute to select a color and put the hex value inside another input box. I removed the word "Button" because the other id for the input box that I want to have the hex color value inside is the same except without the word "Button".

    Code:
        headerBGColor = colorButton('headerBGColorButton');
    
        function colorButton(buttonId) {
            // Get element id value from buttonId
            var idButtonValue = document.getElementById(buttonId).value;
            // Remove "Button" from the id name        
            var idButtonRemoved = buttonId.replace('Button', '');
            // Get element id from idValueInputName
            var idValueInputName = document.getElementById(idButtonRemoved);
            // Set value attribute of input box to be the same as the color selected from button
            var idValueSet = idValueInputName.setAttribute('value', idButtonValue);
            //  Return value
            return idValueSet;     
        };
        console.log(headerBGColor);

  • #5
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    3,020
    Thanks
    56
    Thanked 566 Times in 563 Posts
    it's kind of confusing, and I'm guessing at your html here, but something like this?

    Code:
    <body>
    <input type="button" value="Button#00FF7F" id ='headerBGColorButton'/>
    <input id ='headerBGColor'/>
    <script  type="text/javascript">
    headerBGColor = colorButton('headerBGColorButton');
    
        function colorButton(buttonId) {
            // Get element id value from buttonId
            var button = document.getElementById(buttonId);
            // Remove "Button" from the id name        
            var idButtonRemoved = button.id.replace('Button', '');
    		var valueButtonRemoved = button.value.replace('Button', '');
            // Get element id from idValueInputName
            var idValueInputName = document.getElementById(idButtonRemoved);
            // Set value attribute of input box to be the same as the color selected from button
            idValueInputName.setAttribute('value', valueButtonRemoved);
            //  Return value
            return valueButtonRemoved;     
        };
        console.log(headerBGColor);
    </script>
    </body>

  • Users who have thanked xelawho for this post:

    MRW (08-10-2014)

  • #6
    MRW
    MRW is offline
    New to the CF scene
    Join Date
    Aug 2014
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks for the help. That worked perfectly.


  •  

    Posting Permissions

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