jQuery selector with missing "#" and "." -


i've got dirty working code function. looks wrong, clean it. here's snippet:

var id = 'foo1';           //$(this).closest('.myclass').attr('id'); var class = 'morefoo';     //$(this).attr("class").split(" ").pop()  $('#' + id + ' .' + class).show(); 

someone have idea how write more intelligent? webstorm doesn't '#' + myvar + ' .' not really.

there's not can here. concatenate , build selector first , assign variable like,

var elmid = 'foo1';  var elmclass = 'morefoo';  var elmsel = '#' + elmid + ' .' + elmclass;  $(elmsel).show(); //this way, can use elmsel instead of concatenating every time 

note: cannot use class variable name it's reserved keyword. moreover, avoid using id variable name it's generic.


Comments

Popular posts from this blog

javascript - Confirm a form & display message if form is valid with JQuery -

Retrieving ETA (estimated time of arrival) with Google Distance Matrix API and public transit as transport mode -

ionic framework - Meteor - Error: Failed to execute 'insertBefore' on 'Node' -