How to concatenate a variable in a string in javascript

I use this

var abc = jQuery('#pl_hid_addon_name').val(); alert(abc); var atLeastOneIsChecked = jQuery('input[name="addon-"'+abc+']:checked').length ; alert(atLeastOneIsChecked); 

But this did not work. It should be after concatenation, as shown below.

 var atLeastOneIsChecked = jQuery('input[name="addon-base-bar2[]"]:checked').length; 
+4
source share
4 answers
 var atLeastOneIsChecked = jQuery('input[name="addon-"'+abc+']:checked').length; ^ | 

You used the closure "in the wrong place"

 var atLeastOneIsChecked = jQuery('input[name="addon-'+abc+'"]:checked').length; ^ | 
+17
source

Coincidence:

 var atLeastOneIsChecked = jQuery('input[name="addon-'+abc+'"]:checked').length ; 
+2
source

Try it -

 var atLeastOneIsChecked = jQuery("input[name='addon-"+abc+"']:checked").length ; 
+2
source

I tried some of the methods suggested above. However, no one was useful to me. After looking at some information, I found a property attribute in jQuery (prop ()). And this works for me, the code is as follows:

In my JSF file, I had the following code.

 <h:selectOneMenu id="asignacion" value="#{contratosBean.asignacion}"> <f:selectItems value="#{contratosController.asignaciones}" var="item" itemLabel="#{item.lblAsignacion}" itemValue="#{item.idAsignacion}" /> <f:ajax onevent="showDialog()" /> 

JavaScript section:

 function showDialog() { if($([id='formContrato:asignacion']").prop("selected",true).val() == 'X1') { alert("function X1"); }else if($("id='formContrato:asignacion']").prop("selected",true).val() == 'X2'){alert("function X2"); }else{alert("Other function");} } 
0
source

All Articles