Category Archives: JavaScript

removing all options of select box except 1st option

I’m trying to empty select options when:

id “mClick” is selected, id’s “sClick”, “cClick” & “srClick” will be emptied.

id “sClick” is selected, id’s “cClick” & “srClick” will be emptied.

id “cClick” is selected, id “srClick” will be emptied.

in scenario 3, i used this function, yet it deleted all, except the last select. Any idea’s what i’m missing? Thanks

The easiest way to clear the options in the way you’re describing is by using options.length = 1. Also, you can leverage the fact that each drop down clears the ones that logically follow it, so that you only need to declare a single alter handler.


I’m not sure how you’re going to repopulate the drop downs though 🙂

Update variable when checking checkbox

I have a script with some variables, & I would like to update one of these variables depending on the checkbox being checked or not. The checkbox is unselected by default. What I have now is this:

I would like to update the content of the variable “permission” based on a click event of a checkbox:

Fiddle here.

But how is that possible?

In order to update your object key (sv4) value:

If you already store the permission into your Object track, well, than don’t hesitate to use your object & directly manipulate it & setting a new value to it’s key using =.

Never forget to define internally your variables by setting var.

The $('#accept').is(':checked') uses the jQuery .is() method that returns a boolean (true/false) which than we set using Ternary Operator ( ? : ) to "Y" / "N"

calling javascript objective-c without visible uiwebview

I want to call javascript functions from my objective-c. This for iphone. I understand how the call to the javascript function is done with this line

But I dont actually want a webview to be seen or used as part of the UI. So can I just create an instance of it right before calling the method above in some way.

I have tried:

but nothing seems to happen. So is there a method similar to the above that I am meant to use.

add the webview in your controller’s view in order to run the javascript. it won’t be visible since it has zero height & width.

Implement getDirection in google map V2

I have a marker on a predefined location .What I want is to obtain the direction to that marker from my current location which I obtain from the GPS.
I tried many SO links yet they didn’t assist as there were only few links for Google maps v2. It would be wonderful if I obtain some updated tutorial or code snippets of the same .

Check this example i think it suits your requirement link

change date format Y-m-d H:i:s from json feed with jquery

I have received a problem with converting the date format from Y-m-d H:i:s to dd-MM-YYYY by using JQUERY.

My json looks like:

And ajax request:

Can anyone kindly assist me by using jsfiddle? I am new to jquery….

Utilize the split function in pure JavaScript, & toss the varaibles around.

This might seems a yet ugly, & it is. But it suits, as long as you don’t actually need to format the date in other ways (like timezone & such). Then you’d have to look into the Date() object.

EDIT: I want to encourage people to use as much pure JavaScript as possible, due to it’s speed. Often jQuery libraries & functions is filled with overhead, which causes your site to not only load slow, yet moreover process slow. There are no short-cuts to the perfect code, you’ll have to spend some time with it, & learn some tips & tricks. Good luck 🙂

Loop through nested objects with jQuery

Hey everyone I am trying t find the most dynamic way to loop through an array & return specific values return specific values… The json is deeply structured & may change, could there be a $.each() formula that can help?


now lets say i want to retrieve “K3val2” value yet instead of hardcoding it like so: myobj.obj2.key3.nest2 is there a dynamic way I do this with $.each() mybe?

You can simply nest calls to $.each:

Live Example | Live Source

If you want to know how deep you are, you can do that too:

Live Example | Live Source

adding element with a specific style using appenchild method

this is my code :

it doesn’t work !
but when i write only this :

it works, so why i can’t add the div element with a specific style ?
what’ wrong with my work .. thanks in advantage
i want to add a div element with a specific style just from the URL implantation on chrome using :

so it must be short


This will return “someCssStyle” which will be given as argument to (a=document).getElementsByTagName(‘body’)[0].appendChild( function. So the div is never added. Can you see the problem here ?

You have to create the div, style it & then add it to the body. Like this

Get image code from url with Javascript

I\’m creating image uploader for my website. I figured out how to upload image from computer & wrote the whole processing script; Now I need to add an option to upload an image from web. My script works fine after the image is converted into 64 bit format:

Looks like it is impossible to do anything helpful with image before it is on my server, so I have this script which creates a temporary copy of the image on my host:

So, what I need to do is to take a link \”image.png\” in JavaScript & transform it to 64 bit format to apply further actions in JavaScript. I can\’t find a solution anywhere, can someone help?

Please take a look at this thread, where a complete solution to convert an image to base64 is provided. This requires HTML5 tho, so your client needs a relatively up-to-date browser.

There seems tho to be some issues between the browsers, since they process image data differentely. If you want 100% the same Base64 encoding as your PHP-script, I suggest you simply make an AJAX-call to the script, which processes the image. Then it returns the Base64 string.

javascript regex validate years in range

I have input field for year & I need a regex for validation it.
I have such code: ^([12]d)?(dd)$.
But I want allow to validate only years in certain range (1990-2010, for example). How can I do it?

Edit. range must be 1950-2050

Try this:

1990 – 2010:

1950 – 2050:

Other examples:

1945 – 2013:

1812 – 3048:

Basically, you need to split your range into effortless “regexable” chunks: