0

I'm trying to pass a string from a "text" field to an MVC action. The form variable is called query. The Javascript code is:

<input type="text" class="form-control input-lg" placeholder="search..." 
name="SearchBar" onkeypress="searchfunction()" />
<script>
function searchfunction()
{
    if (event.keyCode == 13) {
        var url = '"' + document.getElementById("#SearchBar").value + '"';
        window.location = "@Url.Action("~/Home/Search")?query=" + url;

    }
}
</script>

The URL generated from this should be domain/Home/Search?query=[contents of textfield] but for some reason it's generating a URL like domain/search.html?SearchBar=[contents of SearchBar] and I get 0x800a138f - JavaScript runtime error: Unable to get property 'value' of undefined or null reference. I am sure those two are related, but .value is what EVERY example I can find recommends using.

Note: I tried using the getElementById both with # and without, same for the name property of the input.

Should I just be passing the string in the onkeypress event? Am I missing something simple? Is this a bug in MVC 3?

1
  • The "id" attribute in input tag is missing, see my answer below Commented Mar 17, 2014 at 19:13

3 Answers 3

3

Add the "id" attribute:

<input type="text" class="form-control input-lg" placeholder="search..." 
  name="SearchBar" id="SearchBar" onkeypress="searchfunction()" />

And remove the "#" character from getElementById() method param

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

Comments

1

try this,

document.getElementById("SearchBar").value

the previous notation comes from jquery.you cannot use them interchangeably.If you prefer the jquery way that would be

$("#Searchbar").val();

and add an id = "Searchbar" to the textfield.

1 Comment

that got rid of the error, but what in my string assembly process is telling it that there should be ".html" anywhere in that string? there aren't even HTML files on this server. whole thing is running with MVC routes.
1

You Define getElementsById() So If you select name from any html element you have to ..Use :

getElementsByName() ,Its return Array not a particular element as you want to select.

In your Case ( because you use # inside get function, i assume you have to get ID )

# = ID selector used In JQuery not in pure javascript.

apply ID of your Element :

<input type="text" class="form-control input-lg" placeholder="search..." 
name="SearchBar" id='SearchBar' onkeypress="searchfunction()" />
<script>
 function searchfunction()
 {
  if (event.keyCode == 13) {
    var url = '"' + document.getElementById("SearchBar").value + '"';
    window.location = "@Url.Action("~/Home/Search")?query=" + url;

  }
}
</script>

3 Comments

...except it's not getElementById("#SearchBar"); should be getElementById("SearchBar").
@AshishMishra +1, would have marked this one had I refreshed and seen the comments.
its Ohk .. :) @Originalmouse

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.