Antarr Byrd Antarr Byrd - 11 months ago 63
Javascript Question

Invalid flags supplied to RegExp constructor in js.erb file

I'm trying to changes the

src
attribute of my img(s) when they are hovered over using jQuery. But I am getting an error saying ,
Uncaught SyntaxError: Invalid flags supplied to RegExp constructor 'icon_history'


app.js.erb

var hover = <%= image_path 'icon_history-h1' %>;

var unhover = <%= image_path 'icon_history' %>;

$('img.more').hover(function(){
console.log(hover);
$(this).attr('src', hover);
},
function(){
console.log(unhover);
$(this).attr('src', unhover);
});

Answer Source

You should escape strings and put them to quotes:

var hover = '<%= j image_path 'icon_history-h1' %>';

var unhover = '<%= j image_path 'icon_history' %>';