Jquery Common function to hide elements by attribute -


i have many checkboxes , related textboxes in page. textboxes hidden default , show if related checkbox checked. how should write common function this, instead of function every checkbox.

http://jsfiddle.net/6scmc/

something..like..:

$("input[type=checkbox]").click( function() {     var x = $(this).attr("name"); $("[id=x]").toggle(this.checked); }); 

the 'name' of checkbox id of textbox.

you dont need attribute selector in context, since targeting id, use id selector below,

try,

$("#" + $(this).attr("name")).toggle(this.checked); 

full code,

$("input[type=checkbox]").change( function() {    $("#" + $(this).attr("name")).toggle(this.checked); }); 

Comments

Popular posts from this blog

android - Inheriting from Theme.AppCompat* -

broadcastreceiver - android BOOT_COMPLETED not received if not activity intent-filter -

basic authentication with http post params android -