admin管理员组

文章数量:1356908

I am new to JavaScript and as part of my project for college I need to include some. I created a simple .js file and linked it to my index.html file using <script src="main.js"></script>

I created another page called sellYourHome.html and added a <section> tag with a table and created a simple form that calculates a ission and displays the results back in the input field.

function Calc()
{
 prcHouse = parseFloat(document.getElementById('prcHouse').value);
 mision =  0.25; 
 result = prcHouse * mision;
 document.getElementById('prcHouse').value = result.toString();
}

That worked.

Now under that function, I wanted to create another one that picks up a <div> element and changes the background-color to, say, blue.

So I created a form on index.html and linked the same .js file to it in the same way.

This is my HTML:

<div id="mainContent">
    <p>hello</p>
    <form id="bgColor">
        <table>
            <tr>
                <td>
                    <input type="button" value="blue" onclick="bgColor('blue');">
                </td>
            </tr>
        </table>
    </form><!-- end of bgColor Form-->
</div>

My javascript is like so:

function Calc()
{
    prcHouse = parseFloat(document.getElementById('prcHouse').value);
    mision =  0.25;  
    result = prcHouse * mision;
    document.getElementById('prcHouse').value = result.toString();
}

function bgColor(color)
{
  var div = document.getElementById('mainContent');
  div.style.background=color;  
}

When I run this, I get this error on the console:

Uncaught TypeError: String is not a function

I am running this in a browser called SWRIron (which is based on Chrome and is HTML5-plient).

What am I doing wrong?

I am new to JavaScript and as part of my project for college I need to include some. I created a simple .js file and linked it to my index.html file using <script src="main.js"></script>

I created another page called sellYourHome.html and added a <section> tag with a table and created a simple form that calculates a ission and displays the results back in the input field.

function Calc()
{
 prcHouse = parseFloat(document.getElementById('prcHouse').value);
 mision =  0.25; 
 result = prcHouse * mision;
 document.getElementById('prcHouse').value = result.toString();
}

That worked.

Now under that function, I wanted to create another one that picks up a <div> element and changes the background-color to, say, blue.

So I created a form on index.html and linked the same .js file to it in the same way.

This is my HTML:

<div id="mainContent">
    <p>hello</p>
    <form id="bgColor">
        <table>
            <tr>
                <td>
                    <input type="button" value="blue" onclick="bgColor('blue');">
                </td>
            </tr>
        </table>
    </form><!-- end of bgColor Form-->
</div>

My javascript is like so:

function Calc()
{
    prcHouse = parseFloat(document.getElementById('prcHouse').value);
    mision =  0.25;  
    result = prcHouse * mision;
    document.getElementById('prcHouse').value = result.toString();
}

function bgColor(color)
{
  var div = document.getElementById('mainContent');
  div.style.background=color;  
}

When I run this, I get this error on the console:

Uncaught TypeError: String is not a function

I am running this in a browser called SWRIron (which is based on Chrome and is HTML5-plient).

What am I doing wrong?

Share Improve this question edited Nov 27, 2013 at 20:05 The Alpha 146k29 gold badges293 silver badges311 bronze badges asked Nov 27, 2013 at 19:35 StudentwebCodingStudentwebCoding 711 gold badge1 silver badge3 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 5

It's because of your function name bgColor, because bgcolor is an attribute of HTML element and it's clashing with your function bgcolor, browser treats this as an attribute (string) but you used it as a function, so it says string is not a function. So, change the function name to anything else like this

function setBgColor(color)
{
    var div = document.getElementById('mainContent');
    div.style.background = color;
}

Use it as

<input type="button" value="blue" onclick="setBgColor('blue');" />

Example.

本文标签: javascriptUncaught TypeError String is not a functionStack Overflow