0

I am trying to concatenate the value of the input element into a string in jquery

I am having trouble concatenate the value of the input element to the end of the string.

Using just companyString or #companyString etc is not working

Below is my code

 $('#search').click(function() {
 $.getJSON('example.php?ol=searchname&name=' + '#companyString'});

Below is my html

Name: <input type="text" id="companyString" value=" ">
 <button type="button" id="search">Search</button>

2 Answers 2

2

You need wrap the string with jQuery object of the input element.

 $('#companyString').val()
Sign up to request clarification or add additional context in comments.

Comments

1

Proper way to take input box value is

$('#companyString').val();    

For eg :

$('#search').click(function() {
    $.getJSON('example.php?ol=searchname&name=' + $('#companyString').val();    ,function(data)

1 Comment

Nice answer with good example. Probably worth mentioning for readability's sake - where long strings are concerned, to store the value in a variable and use it in the + variableName concatenation :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.