0

The problem is that I have a HTML form with 4 text inputs, I submit it to a javascript function and the first 3 inputs work just fine but the fourth is undefined.

Here is the important code: The HTML part:

<form action="insertServer.php" method="post" id="insertServer">
Navn:       <input type="text" name="navn" id="navn">
Netid:      <input type="text" name="netid" id="netid">
Serverid:   <input type="text" name="serverid" id="serverid">
Location:   <input type="text" name="location" id="location">
<input type="submit" name="submitButton" class="button" id="submitButton" value="Indsend">
</form>
<div class="resultInsert"></div>

The Javascript part:

<script type='text/javascript'> //Add server
$("#insertServer").submit(function(event) {
event.preventDefault();

$("div.resultInsert").html("Location is:"+location.value+" navn is: "+navn.value);

});
</script>

When I type data into the fields and press submit the first 3 values are just fine but the fourth is shown as "undefined"

In case you need to see the entire file, I have created a gist for it here: https://gist.github.com/anonymous/8052481

Thanks

2 Answers 2

5

Never try to access an element through the DOM by using its id as if it was a global variable.

Some browsers make this possible (as a very ugly legacy of Internet Explorer 4's), but it isn't reliable.

In this case, location is the standard location object and not your HTML element.

Get the element through the DOM using getElementById, jQuery's selector engine or something similar.

var location = jQuery('#location').val();
var navn = jQuery('#navn').val();

$("div.resultInsert").text("Location is:" + location + " navn is: " + navn);

Also note that I've used the text method instead of the html method. Someone might type something with special meaning in HTML into the inputs. Don't let that break the output.

Sign up to request clarification or add additional context in comments.

Comments

2

location (address object) is predefined variable and cannot be used in other way.

$("#insertServer").submit(function(event) {
event.preventDefault();

$("div.resultInsert").html("Location is:"+$('#location').val()+" navn is: "+$('#navn').val());

});

1 Comment

Thank you, that worked but I would select Quentin's answer because it is more informative and have a better solution for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.