Combine to get new variable name [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I need to get this: $('#anchor')
Where anc hor are variables themselves.
var firstPart = "anc"
var secondPart = "hor"
How do I do that? + to the question (sorry for that) if I have this structure:
<a id="233"></a><li>qwerty</li>
how do I addClass to this exact li that comes after #233?

First part:
$('#' + firstPart + secondPart); // $('#anchor')
Second part:
$('#123 + li').addClass('myclass');
http://www.w3.org/TR/CSS2/selector.html#adjacent-selectors

Answer1
var firstPart = "anc"
var secondPart = "hor"
var selector = "#" + firstPart + secondPart;
Then you can call it by $(selector) .
Answer 2:
$(function(){
$('#233 + li').addClass('ownclass');
});
Check http://jsfiddle.net/alaminopu/U2q6E/ for Answer 2.

'#anchor' is just a string. You use standard string concatination.
var str = '#' + firstPart + secondPart;
$(str)

Related

Need help to understand Vue.js function [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have an assignment to make calculator using vue.js.
Much of it works, I'm confused about how to implement the '=' function.
I found this piece of code in an article that does something similar but I don't understand what it does. Can someone explain it?
https://scotch.io/tutorials/build-a-scientific-calculator-with-vuejs
I found this Piece of code:
if ((app.current).indexOf("^") > -1) {
var base = (app.current).slice(0, (app.current).indexOf("^"));
var exponent = (app.current).slice((app.current).indexOf("^") + 1);
app.current = eval("Math.pow(" + base + "," + exponent + ")");
} else {
app.current =app.current
}
Can someone please explain what the above function does, line-by-line?
// checks if app.current have '^' by getting the index
// if the method indexOf doesn't find anything it return -1
if (app.current.indexOf('^') > -1) {
// gets the first element till the index where it found '^'
var base = app.current.slice(0, app.current.indexOf('^'));
// gets the number after the 'ˆ'
var exponent = app.current.slice(app.current.indexOf('^') + 1);
// eval is evil
// it gets the string and transfoms into valid code
// wich means to do the operation
app.current = eval('Math.pow(' + base + ',' + exponent + ')');
} else {
// if it doesn't find the index it keeps the same value
app.current = app.current;
}
https://javascriptweblog.wordpress.com/2010/04/19/how-evil-is-eval/

find all substrings between ${ and } in javascript [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
i have collections of text with some text between ${ and } like "this is ${test} string ${like}". How can I extract all there strings. Output : test,like
try
match(/{[\w\d]+}/g);
example
"{asdas}32323{234}".match(/{[\w\d]+}/g); //outputs ["{asdas}", "{234}"]
It will return with { and } with the matches which you can remove from the resultset by
"{asdas}32323{234}".match(/{[\w\d]+}/g).map(function(value){return value.substring(1, value.length-1)}); //outputs ["asdas", "234"]
you can try:
"this is ${test} string ${like}".match(/\${\w*}/g).map(function(str){return str.slice(2,-1)})
//["test", "like"]
Try this
var str = "this is ${test} string ${like}";
var txt = str.match(/{[\w\d]+}/g);
for(var i=0; i < txt.length; i++) {
txt[i] = txt[i].replace(/[{}]/g, '');
alert(txt[i]);
}

Syntax error js object to json [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Hello i'm using Backbone here i have an issue, my 'history' attribute will be
history = history + ' * ' + 'Attribute "used" modified from ' + !this.changed.used + ' to ' + this.changed.used + ' by ' + LOGGED_USER.get("fisrtName") + " " + LOGGED_USER.get("name") + ' the ' + (new Date());
But when i do
this.set("history", history);
var json = JSON.stringify(this.toJSON())
the json returned is kind of corrupted with \
Any idea ?
Because the " needs to be escaped in strings, it is not corrupted.
{ "key" : "I am \"valid\"" }

How to execute if condition based on a value from query string in javascript/jquery? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I've a variable called url as follows :
url = "http://56.177.59.250/static/ajax.php?core[ajax]=true&core[call]=prj_name.contactform&width=400&core[security_token]=c7854c13380a26ff009a5cd9e6699840"
Now I want to use if condition only if core[call] is equal to the value it currently has i.e. prj_name.contactform otherwise not.
How should I do this since the parameter from query-string is in array format?
Just use String.indexOf and check if it is present (that is not -1, which means it doesn't exist)
if(url.indexOf("core[call]=prj_name.contactform") > -1){
// valid. Brew some code here
}
You can use location.search :
<script>
function get(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
if(get("core[call]") == "prj_name.contactform"){
alert('ok');
}else{
alert('no');
}
</script>

Some <div>s with a for loop [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a for loop and I want to create many <div>s with it.
I want to give the <div>s am ID from a varible.
Here's the code:
var text = $("#cont").text();
count = 1;
for (var i = 1; i <= 10; i++) {
text += "<div class='ball' id='ball'" + count + "></div><br />";
count++;
}
You've messed up your quotes:
text += "<div class='ball' id='ball" + count + "'></div><br />";
Your code had the single quote wrong and produced this:
<div class='ball' id='ball'5>
The number needs to be inside.
I'm a bit confused what your question is: It looks like you've already got the logic to do what you've specified.
That said, I see a probable error that may be confusing you: You're closing the single-quote on id='ball' before you concatenate the count variable. That will result in the div element not having that number as part of its id. Here's a corrected version of the code:
var text = $("#cont").text();
count = 1;
for (var i = 1; i <= 10; i++) {
text += "<div class='ball' id='ball" + count + "'></div><br />";
count++;
}

Categories

Resources