Skip to content

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 &#8…

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.m…

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 tri…

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