javascript - Toggle issue using input value as id -



javascript - Toggle issue using input value as id -

i'm trying utilize jquery's .toggle() show , hide 1 of 3 divs. divs have unique ids , decision div got toggled based on of 3 radio buttons has been selected. 3 radio buttons have values correspond ids of divs. if clicks -1 radio, div id cmb_0292_a07.m1 should toggled.

however i'm not getting response @ , there's no study of errors in debugger i've tried. wrong?

<!doctype html> <html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script> function showdiv(obj) { var n = obj.name; var v = $("input:radio[name='" + n + "']:checked").val(); alert(v); $("#" + v).toggle(); } </script> </head> <body> <input name="cmb_0292_a07" value="cmb_0292_a07.m1" onclick="showdiv(this);" type="radio">-1 <input name="cmb_0292_a07" value="cmb_0292_a07.0" onclick="showdiv(this);" type="radio">0 <input name="cmb_0292_a07" value="cmb_0292_a07.p1" onclick="showdiv(this);" type="radio">+1 <div id="cmb_0292_a07.m1" style="display: none">minus1</div> <div id="cmb_0292_a07.0" style="display: none">zero</div> <div id="cmb_0292_a07.p1" style="display: none">plus1</div> </body> </html>

this two part issue

first off, shouldn't have . within ids.

secondly, jquery seeing this: $('#id.class') searching dom looking id: cmb_0292_a07then kid class of m1 example.

you can prepare this, either removing periods within ids, or using regex selector [id=""].

$('[id="' + v + '"]').toggle();

jsfiddle demo

other sidenotes, don't utilize onclick events. it's improve separate business logic & presentation logic. utilize jquery .on('click', function () {}); events!

javascript jquery toggle

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -