This question already has answers here:
How can I convert a string to boolean in JavaScript?
(102 answers)
Closed 6 years ago.
I read out a data-attibute, then I want to convert the string "true" into a boolean. At the moment I have to do a comparison in the javascript, is there a better way to do it? I don't know how to use this solution
HTML
<div data-nav='{ "nav": "true"}'>
JS
var data = JSON.parse($nav.attr('data-nav').toString());
data.nav = (data.nav === "true") ? true : false;
Try
<div id='test' data-nav='true'>
var truefalse = $('#test').data('nav');
.data should be able to evaluate it as boolean
here is an example in JSFiddle
https://jsfiddle.net/lismore/hx3gLvgw/
Related
This question already has answers here:
Check if an array contains any element of another array in JavaScript
(32 answers)
Closed 1 year ago.
I've tried the format like
['mobile-number', 'name', 'about'].includes(stages)
But that doesn't seem to work.
if (stages.includes('mobile-number') || stages.includes('name') || stages.includes('about')) {
array.push('about-you')
}
Your looking for Array.prototype.some
stages.some(s => ['mobile-number', 'name', 'about'].includes(s))
This is the exact alternative of stages.includes('mobile-number') || stages.includes('name') || stages.includes('about')
This question already has answers here:
How can I convert a string to boolean in JavaScript?
(102 answers)
Closed 6 years ago.
I've read numerous examples converting from String to Boolean.
For example,
myString = (myString == "true");
But I'm unable to apply the logic to my code. Can someone help?
CommonConfig.getFeatureFlags['analytics.demo'] returns "true" (Note
the "").(That's how backend returns it)
var FeatureFlag = _.extend(CommonConfig, {
demoFeature: String == CommonConfig.getFeatureFlags['analytics.demo'], //either "true" or "false" but I want to make it to true or false
});
Question: I want to convert from String "true" to boolean. And pass true or false based upon!
Can someone help?
Isn't it:
var FeatureFlag = _.extend(CommonConfig, {
demoFeature: "true" == CommonConfig.getFeatureFlags['analytics.demo'],
});
This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 7 years ago.
I want to pass one value through ajax by taking the values from jQuery. But I am using link so I have problems taking the value. I tried the following,
<a id="addpa" class="ActionPopup" href="http://localhost:49951/admin/assignhome/Add?sPId=7">Add</a>
Jquery Code:
var spId = $("#addpa").prop("href"); // Here i am getting a whole Url
var thequerystring = getParameterByName("sPId");
The result is showing undefined. How to take the value of sPId? Give me ideas..
How to take the value of sPId?
Try using String.prototype.split() , Array.prototype.pop()
var spId = $("#addpa").prop("href").split(/=/).pop();
This question already has answers here:
Determine whether an array contains a value [duplicate]
(18 answers)
Closed 9 years ago.
Is there a built-in function in javascript to do this or this is only the option to go? Please look at the code below:
var arr=[1,3,4,'+','-', or whatever]
function value_check(user_click){
var operators=['+','-','/','*','.']
for (var i=0;i<operators.length;i++){
if (arr[arr.length-1]==operators[i]){var value1='operator found';}
if (user_click==operators[i]){
var value2= value1;alert("consecutive operators"); break;
}
}
}
I think this code achieves what I intend to do but is there a simple and shorter way of doing this. In words, I want to achieve something like this:
if (arr[arr.length-1] && user_click BOTH ARE IN operators array)
alert("consecutive operators)
Yes, there are some options:
JavaScript indexOf()
jQuery.inArray()
arrayName.indexOf() is what you are looking for.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
JavaScript: string contains
I want to check if a string contains a specific number in JavaScript, how can I rewrite the statement s contain d in below code?
var s = '11/14/2012';
var d = '14';
if (s contain d) {
//...
}
My case is similar to Check if a string contains a certain number, but how can I implement this action in JavaScript?
Thanks
The easier way is to use index Of.
if(s.indexOf(d) > -1)
var s = '11/14/2012';
var d = '14';
if (s.match(d)) {
alert('Found');
}