My Jade template -
input#main_search.span2(
style = 'height: 26px; width: 800px;' ,
type = 'text',
readonly='true',
name='searchBar',
value='test'
)
JS file -
$('#searchBar').val('hi')
console.log('sup')
Console output -
sup
But searchBar
value stats at test. What am I doing wrong?
This question is related to
javascript
jquery
pug
You are logging sup
directly which is a string
console.log('sup')
Also you are using the wrong id
The template says #main_search
but you are using #searchBar
I suppose you are trying this out
$(function() {
var sup = $('#main_search').val('hi')
console.log(sup); // sup is a variable here
});
Make sure you have the right selector, and then wait until the page is ready and that the element exists until you run the function.
$(function(){
$('#searchBar').val('hi')
});
As Derek points out, the ID is wrong as well.
Change to $('#main_search')
1) you are calling it wrong way try:
$(input[name="searchBar"]).val('hi')
2) if it doesn't work call your .js file at the end of the page or trigger your function on document.ready event
$(document).ready(function() {
$(input[name="searchBar"]).val('hi');
});
$(document).ready(function() {
$('#main_search').val('hi');
});
You're targeting the wrong item with that jQuery selector. The name
of your search bar is searchBar
, not the id
. What you want to use is $('#main_search').val('hi')
.
Source: Stackoverflow.com