Skip to content
Advertisement

Tag: javascript

Keep only first n characters in a string?

Is there a way in JavaScript to remove the end of a string? I need to only keep the first 8 characters of a string and remove the rest. Answer You are looking for JavaScript’s String method substring e.g. Which returns the string starting at the first character and finishing before the 9th character – i.e. ‘Hiya how’. substring documentation

Split the sentences by ‘,’ and remove surrounding spaces

I have this code: Looks like the whole string has been matched, but where has “b” gone? I would rather expect to get: so that I can do m.shift() with a result like s.split(‘,’) but also with whitespaces removed. Do I have a mistake in the regexp or do I misunderstand String.prototype.match? Answer so finally i went with /(?=S)[^,]+?(?=s*(,|$))/g, which

Add 10 seconds to a Date

How can I add 10 seconds to a JavaScript date object? Something like this: Answer There’s a setSeconds method as well: For a list of the other Date functions, you should check out MDN setSeconds will correctly handle wrap-around cases:

jQuery datatables ajax callback

I am using jQuery DataTables and doing server-side data. I’m trying to call a function when the ajax call returns. I tried inserting this fnCallback2 which calls my function and the original function, but jQuery just throws an error (and doesn’t tell me what the error is) and skips out. I also tried adding the fnInitComplete parameter, but that only

Javascript library for human-friendly relative date formatting [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers. We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations. Closed 7 years ago. Improve this question I’d like to display some dates as relative to the current

Linked Dynamic Radio Buttons HTML Javascript

All, Have used this site a few times before and had some great replies so hopefully someone can help again. I want a set of radio buttons, so that when you click a button – you get another set below it. Then again, when you click one of the 2nd set of buttons, you’ll get a third etc. Currently I

If domain specified not equal to then current URL apply this jQuery as well as pages with same domain

The code below only shows <span> on http://example.com/ but wont show <span> on http://example.com/files/target.html so how can I get it to work on all pages with the specified domain? Please help. Answer What you wrote doesn’t work because window.location returns a Location object, which is a host object. The variable myurl is a string. When comparing a string and an

Advertisement