Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am trying to understand a codrops tutorial. Its essentially a slider with a thumb scroller.
I reached a point in the code where they were setting variables to represent clicked items in the thumb scroller like this.
var $currentTitle = $pg_title.find('h1:nth-child('+(current+1)+')');
var $nextTitle = $pg_title.find('h1:nth-child('+(idx+1)+')');
var $currentThumb = $pg_preview.find('img.pg_thumb:eq('+current+')');
I have never seen notation like this +....+. I have been digging and found examples where people used it in stack like this but I haven't seen anyone explain it could someone explain how +...+ returns the value of the clicked item?
When used with a string operand, it concatenates the two strings. In this case, to create a selector. When used with a number, it works as an addition operator. It is used in both ways here. So, if current == 1,
h1:nth-child('+(current+1)+')' will evaluate first to h1:nth-child('+2+'), which will ultimately evaluate to h1:nth-child(2)
'+' Use for string concatenation
$pg_title.find('h1:nth-child('+(current+1)+')');
Like:
var b = 'def';
If you want to add some other string in 'b' variable then you can use '+' for concatenation
var addSomotherSting ='abc' + b + 'ghi';
alert(addSomotherSting);
Then browser show a alert box with 'addSomotherSting' the out put is : 'abcdefghi'
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I'm not so good with regex or trying to return the side part of a string. Can someone help me figure this out. I have a demo below.
str = "<html><head><script>var x = '123';</script></head></html>";
console.log(str)
// should return var x = '123';
Someone wrote a very good regex for stripping tags:
var strippedStr = str.replace(/(<([^>]+)>)/ig,"");
console.log(strippedStr);
Source: http://css-tricks.com/snippets/javascript/strip-html-tags-in-javascript/
I played around a bit with it and found a way with match using groups:
str.match(/(>)([^><]+)(<\/)/m)[2]
result = "var x = '123';"
=> a range (2nd group) beginning by ">" (1st group) and ending with "var x = '123';
I am not sure it'll cover all the cases...
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I need your help.
One thing that's always going to be known in my function is that my string will always have a -2 at the end.
ie.
var x = filenumber-2
I'd like to use an if statement to check if the string: -2 is attached to the string. If it is just return true.
Since the filenumber value will be a variety of different combinations its length will always need to be accounted for. But as for the the -2 at the end, it is a given.
Simple regex-based solution:
if (/-2$/.test(filenumber)) return true;
Using either the String slice() or substr() methods:
if (filenumber.slice(-2) =="-2") return true;
if (filenumber.substr(-2)=="-2") return true;
The -2 in the method call means "start 2 characters before the end of the string".
if(x.slice(-2) == "-2") return true;
Will this suffice?
Use the negative operator of the slice method. It starts counting backwards from the end of the string, so it doesn't matter how long it is.
if(x.slice(-2) == "-2"){
return true;
}
Or if you really want you could use its length and count from the start
if(x.slice(x.length - 2) == "-2"){
return true;
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
So I am trying to highlight text by selecting it and clicking the button (labeled get highlighted text).
This is the implementation of it:
http://jsbin.com/uzILUro/1
Part of the html that has multiple tags is not getting selected.
i.e. try to select the below paragraph and highlight it by clicking the button.
"The remainder is r when p is divided by k" means p = kq + r; the integer q is called the quotient. For instance, “The remainder is 1 when 7 is divided by 3” means 7 = 3·2 + 1. Dividing both sides of p = kq + r by k gives the following alternative form p/k = q + r/k.
All the other works except the paragraph above.
You could use window.find() in most browsers, although it may end up being removed from browsers in the future (Mozilla, WebKit). Here's an answer about that:
https://stackoverflow.com/a/5887719/96100
The main problem is that you're using var searchText = document.getSelection().toString() which doesn't take into account opening and closing tags inside your selection.
Instead you should try using searchText = newNode.innerHTML
Make these changes in your getSelectedText function, and you should be golden.
Some extra changes might be required for IE, though.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
Say I have a range which consists of < -10 and I split this up using a regex call which leaves me with < -10.
I then have a function which gets me the number from the split and I call it like range1.getMin(), this would return -10 but when I use range1.getMin().indexOf('-') it doesn't work.
Try comparing to zero:
var isNegative = range1.getMin() < 0;
function isMin(value) {
if(value<0) {
return true;
}
return false;
}
You could add a check like eval() for the value to make sure you're dealing with an integer.
Also, if you need to make sure you have a positive number (or negative number for that matter) before you use the number in your process, you can Math.abs() your number to make sure it's always a positive number.
var val = parseInt("-10", 10)
can be used to parse integer and to test for positive number
val >= 0
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have the following element on my page:
<div id="price">$8.00</div>
I want to be able to assign the value 8.00 to a JavaScript variable so that I can perform math on it (specifically 8.00 * .2), and then want to take the new number and place it next to the original, while applying a line-through on the original number ONLY. So the outcome would look something like this:
$8.00 $1.60 (where the "$8.00" has a text-decoration:line-through applied to it.)
I have been able to figure out the math and resetting the text, but I need help figuring out how to take something that's displayed, and make assign its value to a variable. I am using jQuery. Any help is appreciated.
$('#price').text() will get you the text. From there, you can use parseFloat() to get the number. However, you have a $ in there, so you will need substring as well.
var price = parseFloat($('#price').text().trim().substr(1));
You can either use regex to find just the number and use that, or you can get all the information and parseFloat, which will return just the number... Or, if you can modify your html, you can do
<div id="price">$<span id="actual_price">8.00</span></div>
parseFloat($('#price').text()) will give you the number as a floating point number.
however if you want to get a variable you must use var to assign one.
Also, you will need to use substring to just get the 8.00 or the variable will be NaN.
HTML
<div id="price">$8.00</div>
jQuery
var price = $('#price').text().substring(1);
newPrice = price * 0.2;
newText = '<span>$' + price + '</span> $' + newPrice;
$('#price').html(newText);
CSS
#price span { text-decoration: line-through; }