DNAInstant
DNAInstant

Reputation: 137

Accessing the values of HTML forms in Javascript

I am new to Javascript and I am having trouble figuring out why this code does not work the way I think it should.

Here is my Javascript code. All I'm trying to do (for now) is take the values from these text boxes and drop downs, and concatenate them.

<script>
function AdvancedSearch () {
var color = document.getElementByID("AdvSearchColor").value;
var ply = document.getElementByID("AdvSearchPly").value;
var cat = document.getElementByID("AdvSearchCategory").value;
var size = document.getElementByID("AdvSearchSize").value;
var description = document.getElementByID("AdvSearchDescription").value;
var fullsearch = ply + color + cat + description + size;
}
</script>    

<form id="advsearach" onsubmit="AdvancedSearch()">
Product Color: <input type="text" name="productcolor" id="AdvSearchProductColor"><br>
Product Ply Rating: <input type="text" name="productply" id="AdvSearchPlyRating"><br>
Product Category: <input type="text" name="productcategory" id="AdvSearchProductCategory"><br>
Product Size: <input type="text" name="productsize" id="AdvSearchProductSize"><br>
Product Description: <input type="text" name="productdescription" id="AdvSearchProductDescription"><br>
<input type="button" onclick="javascript:AdvancedSearch()" value="Search!">
</form> 

So I'm having trouble on the trivial of problems, just debugging this and getting it to work. Spent a few hours on it already.. Thanks for anyone who takes the time to look at this, it will probably save me a lot of time in the long run and that is a very nice thing to do.

Upvotes: 0

Views: 77

Answers (1)

lukaleli
lukaleli

Reputation: 3627

you should use document.getElementById instead of document.getElementByID. There's a typo "Id" != "ID". And IDs of your form MUST match those in javascript form o.O

<form id="advsearach">
    Product Color: <input type="text" name="productcolor" id="AdvSearchColor"><br>
    Product Ply Rating: <input type="text" name="productply" id="AdvSearchPly"><br>
    Product Category: <input type="text" name="productcategory" id="AdvSearchCategory"><br>
    Product Size: <input type="text" name="productsize" id="AdvSearchSize"><br>
    Product Description: <input type="text" name="productdescription" id="AdvSearchDescription"><br>
    <input type="button" onclick="javascript:AdvancedSearch()" value="Search!">
</form> 

<script>
function AdvancedSearch () {
    var color = document.getElementById("AdvSearchColor").value,
        ply = document.getElementById("AdvSearchPly").value,
        cat = document.getElementById("AdvSearchCategory").value,
        size = document.getElementById("AdvSearchSize").value,
        description = document.getElementById("AdvSearchDescription").value,
        fullsearch = ply + color + cat + description + size;
    console.log(fullsearch);
}
</script>

Upvotes: 2

Related Questions