Skip to content

Non-AJAX jQuery POST request

I am trying to use the jQuery POST function but it is handling the request in AJAX style. I mean it’s not actually going to the page I am telling it to go. This function should go to comments.php page with the aid value in hand. It’s posting fine but not redirecting to comments.php. @Doug Neiner C…

Validate image type using javascript

i have a form like this: Please how can I valid this form using javascript so that only jpg files are uploaded. thanks for reading. Answer You can bind the onsubmit event of your form, and check if the value of your file input ends with “.jpg” or “.jpeg”, something like this: Check the…

How to get the number of lines in a textarea?

What I would like is to count the number of lines in a textarea, e.g: should count up to 4 lines. Basically pressing enter once would transfer you to the next line The following code isn’t working: It always gives ‘1’ no matter how many lines. Answer I have implemented the lines and lineCoun…

How to set DOM element as first child?

I have an element E and I’m appending some elements to it. All of a sudden, I find out that the next element to append should be the first child of E. What’s the trick, how to do it? Method unshift doesn’t work because E is an object, not array. Long way would be to iterate through E’s…

How to remove element from an array in JavaScript?

Remove the first element I want to remove the first element of the array so that it becomes: Remove the second element To extend this question, what if I want to remove the second element of the array so that it becomes: Answer For a more flexible solution, use the splice() function. It allows you to remove a…

JavaScript alert box with timer

I want to display the alert box but for a certain interval. Is it possible in JavaScript? Answer If you want an alert to appear after a certain about time, you can use this code: If you want an alert to appear and disappear after a specified interval has passed, then you’re out of luck. When an alert ha…

How to create an on/off switch with Javascript/CSS?

I want to have a sliding switch. On the left would be Off and on the right would be On. When the user toggles the switch, I want the ‘slider’ portion to slide to the other side and indicate it is off. I could then have a callback that takes as input the state of the toggle switch so I