Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to concatenate variable in string in javascript

I am using this

var abc = jQuery('#pl_hid_addon_name').val(); 
alert(abc);

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

But it not worked It should be after concatenate like below

var atLeastOneIsChecked = jQuery('input[name="addon-base-bar2[]"]:checked').length;
like image 516
user2320325 Avatar asked Apr 30 '13 14:04

user2320325


People also ask

Can you concatenate strings in JavaScript?

The concat() function concatenates the string arguments to the calling string and returns a new string.

What is the best way to concatenate strings in JavaScript?

The best and fastest way to concatenate strings in JavaScript is to use the + operator. You can also use the concat() method.

Can you use += for string concatenation?

Concatenation is the process of combining two or more strings to form a new string by subsequently appending the next string to the end of the previous strings. In Java, two strings can be concatenated by using the + or += operator, or through the concat() method, defined in the java. lang. String class.

How do I concatenate strings?

Concatenation is the process of appending one string to the end of another string. You concatenate strings by using the + operator. For string literals and string constants, concatenation occurs at compile time; no run-time concatenation occurs.


2 Answers

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

You used the closing " at the wrong place

var atLeastOneIsChecked = jQuery('input[name="addon-'+abc+'"]:checked').length;
                                                           ^
                                                           |
like image 118
epascarello Avatar answered Oct 25 '22 07:10

epascarello


Try this -

var atLeastOneIsChecked = jQuery("input[name='addon-"+abc+"']:checked").length ;
like image 45
Mohammad Adil Avatar answered Oct 25 '22 07:10

Mohammad Adil