How to get and split value of the input field in jquery
I have and input search field where I can search for make or/and model of the vehicle and then I want to search depending on that term.
But the problem is how can I split make from model if some makes have more than one word in the name and some only one.
My code is as follows:
<input type="text" placeholder="Search make or model" id="search" />
<button type="submit" id="btn-submit">Search</button>
And javascript:
$("#btn-submit").on("click", function(e){
let make_model = $("#search").val()
let make = make_model.split(/ +/)[0]
})
If I search for make that has one word and model which has one word I can split the string and use the first value as make and the second one as model. Just like in my example.
Thus if I search for Audi A5
I can get make and model as follows:
let make = "Audi A5".split(/ +/)[0]
let model = "Audi A5".split(/ +/)[1]
But the problem is if I search for for example for Alfa Romeo Giulietta
or something like that. How can I then split the string and get correct make and correct model.
If I have the list of all makes, can I somehow compare searched term and check if the make has more than two words?
Any idea?
Here is the fiddle.
const all_makes = ['Alfa Romeo', 'Aston Martin', 'Audi', 'Volkswagen', 'Volvo']
$(function() {
$("#btn-submit").on("click", function(e) {
let make_model = $("#search").val()
let make = make_model.split(/ +/)[0]
console.log(make)
});
});
#search {
width: 200px;
padding: 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" placeholder="Search make or model" id="search" />
<button type="submit" id="btn-submit">Search</button>
javascript jquery
|
show 2 more comments
I have and input search field where I can search for make or/and model of the vehicle and then I want to search depending on that term.
But the problem is how can I split make from model if some makes have more than one word in the name and some only one.
My code is as follows:
<input type="text" placeholder="Search make or model" id="search" />
<button type="submit" id="btn-submit">Search</button>
And javascript:
$("#btn-submit").on("click", function(e){
let make_model = $("#search").val()
let make = make_model.split(/ +/)[0]
})
If I search for make that has one word and model which has one word I can split the string and use the first value as make and the second one as model. Just like in my example.
Thus if I search for Audi A5
I can get make and model as follows:
let make = "Audi A5".split(/ +/)[0]
let model = "Audi A5".split(/ +/)[1]
But the problem is if I search for for example for Alfa Romeo Giulietta
or something like that. How can I then split the string and get correct make and correct model.
If I have the list of all makes, can I somehow compare searched term and check if the make has more than two words?
Any idea?
Here is the fiddle.
const all_makes = ['Alfa Romeo', 'Aston Martin', 'Audi', 'Volkswagen', 'Volvo']
$(function() {
$("#btn-submit").on("click", function(e) {
let make_model = $("#search").val()
let make = make_model.split(/ +/)[0]
console.log(make)
});
});
#search {
width: 200px;
padding: 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" placeholder="Search make or model" id="search" />
<button type="submit" id="btn-submit">Search</button>
javascript jquery
I guess you mean brand and not "make"?
– DoubleVoid
Nov 19 '18 at 14:19
@DoubleVoid Yes.
– Boky
Nov 19 '18 at 14:20
1
Why don't you first explain your use case, ie. what you are trying to achieve? The posted code doesn't make much sense.
– MrUpsidown
Nov 19 '18 at 14:23
1
Okay, since you only have one input field you will not be able to distinguish between brand and model. You can establish something like a tag based search or lookup table.
– DoubleVoid
Nov 19 '18 at 14:23
2
As someone who has worked in the automotive software industry for 7 years, do not split a string into Make, Model, and Trim. While it works for Audi A7, it does not for Land Rover Range Rover (Land Rover being the make and Range Rover being the model) or many other complex examples. You’re going to want to normalize your data and use tables of the normalized data. Alternatively you could purchase normalized data about vehicles from a 3rd party like Blackbook (who I am not affiliated with).
– Nate
Nov 19 '18 at 14:51
|
show 2 more comments
I have and input search field where I can search for make or/and model of the vehicle and then I want to search depending on that term.
But the problem is how can I split make from model if some makes have more than one word in the name and some only one.
My code is as follows:
<input type="text" placeholder="Search make or model" id="search" />
<button type="submit" id="btn-submit">Search</button>
And javascript:
$("#btn-submit").on("click", function(e){
let make_model = $("#search").val()
let make = make_model.split(/ +/)[0]
})
If I search for make that has one word and model which has one word I can split the string and use the first value as make and the second one as model. Just like in my example.
Thus if I search for Audi A5
I can get make and model as follows:
let make = "Audi A5".split(/ +/)[0]
let model = "Audi A5".split(/ +/)[1]
But the problem is if I search for for example for Alfa Romeo Giulietta
or something like that. How can I then split the string and get correct make and correct model.
If I have the list of all makes, can I somehow compare searched term and check if the make has more than two words?
Any idea?
Here is the fiddle.
const all_makes = ['Alfa Romeo', 'Aston Martin', 'Audi', 'Volkswagen', 'Volvo']
$(function() {
$("#btn-submit").on("click", function(e) {
let make_model = $("#search").val()
let make = make_model.split(/ +/)[0]
console.log(make)
});
});
#search {
width: 200px;
padding: 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" placeholder="Search make or model" id="search" />
<button type="submit" id="btn-submit">Search</button>
javascript jquery
I have and input search field where I can search for make or/and model of the vehicle and then I want to search depending on that term.
But the problem is how can I split make from model if some makes have more than one word in the name and some only one.
My code is as follows:
<input type="text" placeholder="Search make or model" id="search" />
<button type="submit" id="btn-submit">Search</button>
And javascript:
$("#btn-submit").on("click", function(e){
let make_model = $("#search").val()
let make = make_model.split(/ +/)[0]
})
If I search for make that has one word and model which has one word I can split the string and use the first value as make and the second one as model. Just like in my example.
Thus if I search for Audi A5
I can get make and model as follows:
let make = "Audi A5".split(/ +/)[0]
let model = "Audi A5".split(/ +/)[1]
But the problem is if I search for for example for Alfa Romeo Giulietta
or something like that. How can I then split the string and get correct make and correct model.
If I have the list of all makes, can I somehow compare searched term and check if the make has more than two words?
Any idea?
Here is the fiddle.
const all_makes = ['Alfa Romeo', 'Aston Martin', 'Audi', 'Volkswagen', 'Volvo']
$(function() {
$("#btn-submit").on("click", function(e) {
let make_model = $("#search").val()
let make = make_model.split(/ +/)[0]
console.log(make)
});
});
#search {
width: 200px;
padding: 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" placeholder="Search make or model" id="search" />
<button type="submit" id="btn-submit">Search</button>
const all_makes = ['Alfa Romeo', 'Aston Martin', 'Audi', 'Volkswagen', 'Volvo']
$(function() {
$("#btn-submit").on("click", function(e) {
let make_model = $("#search").val()
let make = make_model.split(/ +/)[0]
console.log(make)
});
});
#search {
width: 200px;
padding: 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" placeholder="Search make or model" id="search" />
<button type="submit" id="btn-submit">Search</button>
const all_makes = ['Alfa Romeo', 'Aston Martin', 'Audi', 'Volkswagen', 'Volvo']
$(function() {
$("#btn-submit").on("click", function(e) {
let make_model = $("#search").val()
let make = make_model.split(/ +/)[0]
console.log(make)
});
});
#search {
width: 200px;
padding: 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" placeholder="Search make or model" id="search" />
<button type="submit" id="btn-submit">Search</button>
javascript jquery
javascript jquery
edited Nov 19 '18 at 14:23
asked Nov 19 '18 at 14:17


Boky
3,84433676
3,84433676
I guess you mean brand and not "make"?
– DoubleVoid
Nov 19 '18 at 14:19
@DoubleVoid Yes.
– Boky
Nov 19 '18 at 14:20
1
Why don't you first explain your use case, ie. what you are trying to achieve? The posted code doesn't make much sense.
– MrUpsidown
Nov 19 '18 at 14:23
1
Okay, since you only have one input field you will not be able to distinguish between brand and model. You can establish something like a tag based search or lookup table.
– DoubleVoid
Nov 19 '18 at 14:23
2
As someone who has worked in the automotive software industry for 7 years, do not split a string into Make, Model, and Trim. While it works for Audi A7, it does not for Land Rover Range Rover (Land Rover being the make and Range Rover being the model) or many other complex examples. You’re going to want to normalize your data and use tables of the normalized data. Alternatively you could purchase normalized data about vehicles from a 3rd party like Blackbook (who I am not affiliated with).
– Nate
Nov 19 '18 at 14:51
|
show 2 more comments
I guess you mean brand and not "make"?
– DoubleVoid
Nov 19 '18 at 14:19
@DoubleVoid Yes.
– Boky
Nov 19 '18 at 14:20
1
Why don't you first explain your use case, ie. what you are trying to achieve? The posted code doesn't make much sense.
– MrUpsidown
Nov 19 '18 at 14:23
1
Okay, since you only have one input field you will not be able to distinguish between brand and model. You can establish something like a tag based search or lookup table.
– DoubleVoid
Nov 19 '18 at 14:23
2
As someone who has worked in the automotive software industry for 7 years, do not split a string into Make, Model, and Trim. While it works for Audi A7, it does not for Land Rover Range Rover (Land Rover being the make and Range Rover being the model) or many other complex examples. You’re going to want to normalize your data and use tables of the normalized data. Alternatively you could purchase normalized data about vehicles from a 3rd party like Blackbook (who I am not affiliated with).
– Nate
Nov 19 '18 at 14:51
I guess you mean brand and not "make"?
– DoubleVoid
Nov 19 '18 at 14:19
I guess you mean brand and not "make"?
– DoubleVoid
Nov 19 '18 at 14:19
@DoubleVoid Yes.
– Boky
Nov 19 '18 at 14:20
@DoubleVoid Yes.
– Boky
Nov 19 '18 at 14:20
1
1
Why don't you first explain your use case, ie. what you are trying to achieve? The posted code doesn't make much sense.
– MrUpsidown
Nov 19 '18 at 14:23
Why don't you first explain your use case, ie. what you are trying to achieve? The posted code doesn't make much sense.
– MrUpsidown
Nov 19 '18 at 14:23
1
1
Okay, since you only have one input field you will not be able to distinguish between brand and model. You can establish something like a tag based search or lookup table.
– DoubleVoid
Nov 19 '18 at 14:23
Okay, since you only have one input field you will not be able to distinguish between brand and model. You can establish something like a tag based search or lookup table.
– DoubleVoid
Nov 19 '18 at 14:23
2
2
As someone who has worked in the automotive software industry for 7 years, do not split a string into Make, Model, and Trim. While it works for Audi A7, it does not for Land Rover Range Rover (Land Rover being the make and Range Rover being the model) or many other complex examples. You’re going to want to normalize your data and use tables of the normalized data. Alternatively you could purchase normalized data about vehicles from a 3rd party like Blackbook (who I am not affiliated with).
– Nate
Nov 19 '18 at 14:51
As someone who has worked in the automotive software industry for 7 years, do not split a string into Make, Model, and Trim. While it works for Audi A7, it does not for Land Rover Range Rover (Land Rover being the make and Range Rover being the model) or many other complex examples. You’re going to want to normalize your data and use tables of the normalized data. Alternatively you could purchase normalized data about vehicles from a 3rd party like Blackbook (who I am not affiliated with).
– Nate
Nov 19 '18 at 14:51
|
show 2 more comments
3 Answers
3
active
oldest
votes
You can use the list of brands to make a regular expression that can make the split:
const all_makes = ['Alfa Romeo', 'Aston Martin', 'Audi', 'Volkswagen', 'Volvo'];
const regex = new RegExp(`(${all_makes.join('|').replace(' ', '\s+')}|\S+)\s*(.*)`, "i");
$(function() {
$("#btn-submit").on("click", function(e) {
let make_model = $("#search").val();
let [_, make, model] = make_model.match(regex);
console.log("make =", make);
console.log("model =", model);
});
});
#search {
width: 200px;
padding: 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" placeholder="Search make or model" id="search" />
<button type="submit" id="btn-submit">Search</button>
add a comment |
A simple split on spaces won't cut it - you'll have to look for instances of all the makes in your user's string. You can coerce to lowercase to let users be a little sloppy.
Then you'd take the matching make, split the string after it, trim the result, and use it as the model.
If you have a list of all makes, it's probably easier and less likely to result in user error to have them pick from the list.
This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - From Review
– Bonatti
Nov 19 '18 at 16:03
add a comment |
As i assume, you want to console the brand name in your case the make variable.
So checking if the model is present as a substring of array element. If the substring exists, print the substring.
Also note that the search is case sensitive
Sample input
Audi A5
Sample Output
Audi
Sample input
Alfa Romeo Giulietta
Sample Output
Alfa Romeo
const all_makes = ['Alfa Romeo', 'Aston Martin', 'Audi', 'Volkswagen', 'Volvo']
$(function() {
$("#btn-submit").on("click", function(e) {
let make_model = $("#search").val()
let make = make_model.split(/ +/)[0]
var el = all_makes.find(a => a.includes(make));
console.log(el)
});
});
#search {
width: 200px;
padding: 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" placeholder="Search make or model" id="search" />
<button type="submit" id="btn-submit">Search</button>
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53376559%2fhow-to-get-and-split-value-of-the-input-field-in-jquery%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use the list of brands to make a regular expression that can make the split:
const all_makes = ['Alfa Romeo', 'Aston Martin', 'Audi', 'Volkswagen', 'Volvo'];
const regex = new RegExp(`(${all_makes.join('|').replace(' ', '\s+')}|\S+)\s*(.*)`, "i");
$(function() {
$("#btn-submit").on("click", function(e) {
let make_model = $("#search").val();
let [_, make, model] = make_model.match(regex);
console.log("make =", make);
console.log("model =", model);
});
});
#search {
width: 200px;
padding: 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" placeholder="Search make or model" id="search" />
<button type="submit" id="btn-submit">Search</button>
add a comment |
You can use the list of brands to make a regular expression that can make the split:
const all_makes = ['Alfa Romeo', 'Aston Martin', 'Audi', 'Volkswagen', 'Volvo'];
const regex = new RegExp(`(${all_makes.join('|').replace(' ', '\s+')}|\S+)\s*(.*)`, "i");
$(function() {
$("#btn-submit").on("click", function(e) {
let make_model = $("#search").val();
let [_, make, model] = make_model.match(regex);
console.log("make =", make);
console.log("model =", model);
});
});
#search {
width: 200px;
padding: 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" placeholder="Search make or model" id="search" />
<button type="submit" id="btn-submit">Search</button>
add a comment |
You can use the list of brands to make a regular expression that can make the split:
const all_makes = ['Alfa Romeo', 'Aston Martin', 'Audi', 'Volkswagen', 'Volvo'];
const regex = new RegExp(`(${all_makes.join('|').replace(' ', '\s+')}|\S+)\s*(.*)`, "i");
$(function() {
$("#btn-submit").on("click", function(e) {
let make_model = $("#search").val();
let [_, make, model] = make_model.match(regex);
console.log("make =", make);
console.log("model =", model);
});
});
#search {
width: 200px;
padding: 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" placeholder="Search make or model" id="search" />
<button type="submit" id="btn-submit">Search</button>
You can use the list of brands to make a regular expression that can make the split:
const all_makes = ['Alfa Romeo', 'Aston Martin', 'Audi', 'Volkswagen', 'Volvo'];
const regex = new RegExp(`(${all_makes.join('|').replace(' ', '\s+')}|\S+)\s*(.*)`, "i");
$(function() {
$("#btn-submit").on("click", function(e) {
let make_model = $("#search").val();
let [_, make, model] = make_model.match(regex);
console.log("make =", make);
console.log("model =", model);
});
});
#search {
width: 200px;
padding: 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" placeholder="Search make or model" id="search" />
<button type="submit" id="btn-submit">Search</button>
const all_makes = ['Alfa Romeo', 'Aston Martin', 'Audi', 'Volkswagen', 'Volvo'];
const regex = new RegExp(`(${all_makes.join('|').replace(' ', '\s+')}|\S+)\s*(.*)`, "i");
$(function() {
$("#btn-submit").on("click", function(e) {
let make_model = $("#search").val();
let [_, make, model] = make_model.match(regex);
console.log("make =", make);
console.log("model =", model);
});
});
#search {
width: 200px;
padding: 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" placeholder="Search make or model" id="search" />
<button type="submit" id="btn-submit">Search</button>
const all_makes = ['Alfa Romeo', 'Aston Martin', 'Audi', 'Volkswagen', 'Volvo'];
const regex = new RegExp(`(${all_makes.join('|').replace(' ', '\s+')}|\S+)\s*(.*)`, "i");
$(function() {
$("#btn-submit").on("click", function(e) {
let make_model = $("#search").val();
let [_, make, model] = make_model.match(regex);
console.log("make =", make);
console.log("model =", model);
});
});
#search {
width: 200px;
padding: 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" placeholder="Search make or model" id="search" />
<button type="submit" id="btn-submit">Search</button>
answered Nov 19 '18 at 14:47


trincot
118k1481112
118k1481112
add a comment |
add a comment |
A simple split on spaces won't cut it - you'll have to look for instances of all the makes in your user's string. You can coerce to lowercase to let users be a little sloppy.
Then you'd take the matching make, split the string after it, trim the result, and use it as the model.
If you have a list of all makes, it's probably easier and less likely to result in user error to have them pick from the list.
This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - From Review
– Bonatti
Nov 19 '18 at 16:03
add a comment |
A simple split on spaces won't cut it - you'll have to look for instances of all the makes in your user's string. You can coerce to lowercase to let users be a little sloppy.
Then you'd take the matching make, split the string after it, trim the result, and use it as the model.
If you have a list of all makes, it's probably easier and less likely to result in user error to have them pick from the list.
This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - From Review
– Bonatti
Nov 19 '18 at 16:03
add a comment |
A simple split on spaces won't cut it - you'll have to look for instances of all the makes in your user's string. You can coerce to lowercase to let users be a little sloppy.
Then you'd take the matching make, split the string after it, trim the result, and use it as the model.
If you have a list of all makes, it's probably easier and less likely to result in user error to have them pick from the list.
A simple split on spaces won't cut it - you'll have to look for instances of all the makes in your user's string. You can coerce to lowercase to let users be a little sloppy.
Then you'd take the matching make, split the string after it, trim the result, and use it as the model.
If you have a list of all makes, it's probably easier and less likely to result in user error to have them pick from the list.
answered Nov 19 '18 at 14:35


pixelbandito
200310
200310
This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - From Review
– Bonatti
Nov 19 '18 at 16:03
add a comment |
This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - From Review
– Bonatti
Nov 19 '18 at 16:03
This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - From Review
– Bonatti
Nov 19 '18 at 16:03
This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - From Review
– Bonatti
Nov 19 '18 at 16:03
add a comment |
As i assume, you want to console the brand name in your case the make variable.
So checking if the model is present as a substring of array element. If the substring exists, print the substring.
Also note that the search is case sensitive
Sample input
Audi A5
Sample Output
Audi
Sample input
Alfa Romeo Giulietta
Sample Output
Alfa Romeo
const all_makes = ['Alfa Romeo', 'Aston Martin', 'Audi', 'Volkswagen', 'Volvo']
$(function() {
$("#btn-submit").on("click", function(e) {
let make_model = $("#search").val()
let make = make_model.split(/ +/)[0]
var el = all_makes.find(a => a.includes(make));
console.log(el)
});
});
#search {
width: 200px;
padding: 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" placeholder="Search make or model" id="search" />
<button type="submit" id="btn-submit">Search</button>
add a comment |
As i assume, you want to console the brand name in your case the make variable.
So checking if the model is present as a substring of array element. If the substring exists, print the substring.
Also note that the search is case sensitive
Sample input
Audi A5
Sample Output
Audi
Sample input
Alfa Romeo Giulietta
Sample Output
Alfa Romeo
const all_makes = ['Alfa Romeo', 'Aston Martin', 'Audi', 'Volkswagen', 'Volvo']
$(function() {
$("#btn-submit").on("click", function(e) {
let make_model = $("#search").val()
let make = make_model.split(/ +/)[0]
var el = all_makes.find(a => a.includes(make));
console.log(el)
});
});
#search {
width: 200px;
padding: 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" placeholder="Search make or model" id="search" />
<button type="submit" id="btn-submit">Search</button>
add a comment |
As i assume, you want to console the brand name in your case the make variable.
So checking if the model is present as a substring of array element. If the substring exists, print the substring.
Also note that the search is case sensitive
Sample input
Audi A5
Sample Output
Audi
Sample input
Alfa Romeo Giulietta
Sample Output
Alfa Romeo
const all_makes = ['Alfa Romeo', 'Aston Martin', 'Audi', 'Volkswagen', 'Volvo']
$(function() {
$("#btn-submit").on("click", function(e) {
let make_model = $("#search").val()
let make = make_model.split(/ +/)[0]
var el = all_makes.find(a => a.includes(make));
console.log(el)
});
});
#search {
width: 200px;
padding: 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" placeholder="Search make or model" id="search" />
<button type="submit" id="btn-submit">Search</button>
As i assume, you want to console the brand name in your case the make variable.
So checking if the model is present as a substring of array element. If the substring exists, print the substring.
Also note that the search is case sensitive
Sample input
Audi A5
Sample Output
Audi
Sample input
Alfa Romeo Giulietta
Sample Output
Alfa Romeo
const all_makes = ['Alfa Romeo', 'Aston Martin', 'Audi', 'Volkswagen', 'Volvo']
$(function() {
$("#btn-submit").on("click", function(e) {
let make_model = $("#search").val()
let make = make_model.split(/ +/)[0]
var el = all_makes.find(a => a.includes(make));
console.log(el)
});
});
#search {
width: 200px;
padding: 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" placeholder="Search make or model" id="search" />
<button type="submit" id="btn-submit">Search</button>
const all_makes = ['Alfa Romeo', 'Aston Martin', 'Audi', 'Volkswagen', 'Volvo']
$(function() {
$("#btn-submit").on("click", function(e) {
let make_model = $("#search").val()
let make = make_model.split(/ +/)[0]
var el = all_makes.find(a => a.includes(make));
console.log(el)
});
});
#search {
width: 200px;
padding: 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" placeholder="Search make or model" id="search" />
<button type="submit" id="btn-submit">Search</button>
const all_makes = ['Alfa Romeo', 'Aston Martin', 'Audi', 'Volkswagen', 'Volvo']
$(function() {
$("#btn-submit").on("click", function(e) {
let make_model = $("#search").val()
let make = make_model.split(/ +/)[0]
var el = all_makes.find(a => a.includes(make));
console.log(el)
});
});
#search {
width: 200px;
padding: 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" placeholder="Search make or model" id="search" />
<button type="submit" id="btn-submit">Search</button>
edited Nov 19 '18 at 14:47
answered Nov 19 '18 at 14:42
Monica Acha
2587
2587
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53376559%2fhow-to-get-and-split-value-of-the-input-field-in-jquery%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I guess you mean brand and not "make"?
– DoubleVoid
Nov 19 '18 at 14:19
@DoubleVoid Yes.
– Boky
Nov 19 '18 at 14:20
1
Why don't you first explain your use case, ie. what you are trying to achieve? The posted code doesn't make much sense.
– MrUpsidown
Nov 19 '18 at 14:23
1
Okay, since you only have one input field you will not be able to distinguish between brand and model. You can establish something like a tag based search or lookup table.
– DoubleVoid
Nov 19 '18 at 14:23
2
As someone who has worked in the automotive software industry for 7 years, do not split a string into Make, Model, and Trim. While it works for Audi A7, it does not for Land Rover Range Rover (Land Rover being the make and Range Rover being the model) or many other complex examples. You’re going to want to normalize your data and use tables of the normalized data. Alternatively you could purchase normalized data about vehicles from a 3rd party like Blackbook (who I am not affiliated with).
– Nate
Nov 19 '18 at 14:51