Narabhut Narabhut - 5 months ago 11
jQuery Question

Set value of textbox using JQuery

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?

Answer

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
});