Pages

Friday, 26 April 2013

Javascript, Jquery : How to get all elements which name starts with some string?


A quick and easy way is to use jQuery and do this:

var $eles = $(":input[name^='q1_']").css("font-color","yellow");

That will grab all elements whose name attribute starts with 'q1_'. To convert the resulting collection of jQuery objects to a DOM collection, do this:
 
var DOMeles = $eles.get();
 
see http://api.jquery.com/attribute-starts-with-selector/

In pure DOM, you could use getElementsByTagName to grab all input elements, and loop through the resulting array. Elements with name starting with 'q1_' get pushed to another array:
var eles = [];
var inputs = document.getElementsByTagName("input");
for(var i = 0; i < inputs.length; i++) {
if(inputs[i].name.indexOf('q1_') == 0) {
eles
.push(inputs[i]);
}
}
 
 
 
You can use getElementsByName("input") to get a collection of all the 
inputs on the page. Then loop through the collection, checking the name
on the way. Something like this:

 <!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JS Bin</title>

</head>
<body>

<input name="q1_a" type="text" value="1A"/>
<input name="q1_b" type="text" value="1B"/>
<input name="q1_c" type="text" value="1C"/>
<input name="q2_d" type="text" value="2D"/>

<script type="text/javascript">
var inputs = document.getElementsByTagName("input");
for (x=0;x<=inputs.length;x++){
myname
= inputs[x].getAttribute("name");
if(myname.indexOf("q1_")==0){
alert
(myname);
// do more stuff here
}
}
</script>
</body>
</html>
 
Demo 

Javascript : get all the object where id is like log_XXXX

Easy with jQuery

$("*[id\^='log\_']")
 
Otherwise...
var matches = [];
var elems = document.getElementsByTagName("*");
for (var i=0; i<elems.length; i++) {
if (elems[i].id.indexOf("log_") == 0)
matches
.push(elems[i]);
}
//matches now is an array of all matching elements.
 
 

Ok, here's a straight JavaScript answer:
 
/ a handy function to aid in filtering:
// takes an array and a function, returns another array containing
// only those elements for which f() returns true
function filter(a, f)
{
var ret = [];
for (var i=0; i<a.length; ++i)
{
if ( f(a[i]) )
ret
.push(a[i]);
}
return ret;
}

// this collects all elements in the current document
var elements = document.getElementsByTagName("*");

// and this filters out all but those that match our pattern
var logElements = filter(elements, function(el)
{ return /log_/.test(el.id) } ); // simple expression
 

PHP convert decimal (Float ) into fraction


if you want as like below : 
 

(.5); // return "1/2"

 then
 
call 

decimal_to_fraction(.5);

 
function decToFraction($float)
{
$whole = floor ( $float );
$decimal = $float - $whole;
$leastCommonDenom = 48; // 16 * 3;
$denominators = array (2, 3, 4, 8, 16, 24, 48 );
$roundedDecimal = round ( $decimal * $leastCommonDenom ) / $leastCommonDenom;
if ($roundedDecimal == 0)
return $whole;
if ($roundedDecimal == 1)
return $whole + 1;
foreach ( $denominators as $d ) {
if ($roundedDecimal * $d == floor ( $roundedDecimal * $d )) {
$denom = $d;
break;
}
}
return ($whole == 0 ? '' : $whole) . " " . ($roundedDecimal * $denom) . "/" . $denom;
}
 

JavaScript Array Delete Elements

Q. What is the difference between using the delete operator on the array element as opposed to using the Array.splice method? For example:



myArray = ['a', 'b', 'c', 'd'];

delete myArray[1];
// or
myArray
.splice (1, 1);
 
 

Ans.

Delete won't remove the element from the array it will only set the element as undefined.
So,

> myArray = ['a', 'b', 'c', 'd']
["a", "b", "c", "d"]
> delete myArray[0]
true
> myArray
[undefined, "b", "c", "d"]
> myArray.splice(0, 2)
[undefined, "b"]
> myArray
["c", "d"]
 
 

Array.remove() Method

John Resig, creator of jQuery created a very handy Array.remove method that I always use it in my projects.
// Array Remove - By John Resig (MIT Licensed)
Array.prototype.remove = function(from, to) {
var rest = this.slice((to || from) + 1 || this.length);
this.length = from < 0 ? this.length + from : from;
return this.push.apply(this, rest);
};
and here's some examples of how it could be used:
// Remove the second item from the array
array
.remove(1);
// Remove the second-to-last item from the array
array
.remove(-2);
// Remove the second and third items from the array
array
.remove(1,2);
// Remove the last and second-to-last items from the array
array
.remove(-2,-1);

How do I remove objects from a javascript associative array?

Use the "delete" keyword in Javascript.

delete myArray["lastname"];

Checking if an associative array key exists in Javascript

How do I check if a particular key exists in a Javascript associative array?

If a key doesn't exist and I try to access it, will it return false? Or throw an error?

It will return undefined.
var aa = {hello: "world"};
alert
( aa["hello"] ); // popup box with "world"
alert
( aa["goodbye"] ); // popup boc with "undefined"
undefined is a special constant value. So you can say, e.g.
// note the three equal signs so that null won't be equal to undefined
if( aa["goodbye"] === undefined ) {
// do something
}
This is probably the best way to check for missing keys. However, as is pointed out in a comment below, it's theoretically possible that you'd want to have the actual value be undefined. I've never needed to do this and can't think of a reason offhand why I'd ever want to, but just for the sake of completeness, you can use the in operator
// this works even if you have {"goodbye": undefined}
if( "goodbye" in aa ) {
// do something
}

Thursday, 25 April 2013

How to Make your First iPhone App in XCode 4


How to Make your First iPhone App in XCode 4