Search String
Michael
- javascript
Hi guys,
i got 2 big problems.
The first one is that i wanna search in an array for smth. It works if i use indexOf, but i gotta write the input exactly the same way as it's written in the array. So if the first letter is big in the array and i write a low letter in the input it won't find it. So my question is: how can i make smth like a "like" statement in SQL for javascript?
The 2nd problem is the followin: I wanna get a new div for every one of those outputs, but i don't get. I use a for (var bla bla bla)
var kol = document.getElementById("kol");
var newdiv = document.createElement('div');
newdiv.id = 'hallo';
newdiv.innerHTML = '<div id="klasse1">Search: ' + search\_output[i]["name"];
kol.appendChild(newdiv);
And i defined a css id with "klasse1" and told it that there should be top 5px, but all those divs just still hang together.
Thanks in advance
Michael
Hi Michael,
So my question is: how can i make smth like a "like" statement in SQL for javascript?
Check out the search-Method of the String object.
It allows you to use all kinds of regular expressions, and you can also use modifiers (in your case "i"):
if (myString.search(/smth/i) >= 0) {
// Found
}
That should work (didn't try it :) ).
And i defined a css id with "klasse1" and told it that there should be top 5px, but all those divs just still hang together.
Does your class "klasse1" has a "position" attribute?
Positioning only works if you use "position:absolute" or "position:relative"
(see http://de.selfhtml.org/css/eigenschaften/positionierung.htm#position).
Regards,
Joerg
Hi, thank u for your fast answer.
I allready checked out how to solve the prom with the divs; i had to add it in javascript and not in the css.
But i tried that great idea with search. The prom is how to pass the var?
if i got smth like:
var bla = "How are u doin there?";
var search = "how";
bla.search(...);
How can i now get the case insensitive search?
Thanks a lot guys
Hello,
var bla = "How are u doin there?";
var search = "how";
bla.search(...);
I'm usally doing this with eval:
var bla = "How are u doin there?";
var search = "how";
bla.search(eval("/"+search+"/i"));
This is not very nice ("eval is evil"), but it works.
Regards,
Jörg
[latex]Mae govannen![/latex]
I'm usally doing this with eval:
Yuck :(
var bla = "How are u doin there?";
var search = "how";
bla.search(eval("/"+search+"/i"));
> This is not very nice ("eval is evil"), but it works.
'It works' is no excuse for really bad code.
~~~javascript
var bla = 'How are u doin there?';
var searchstr = 'how';
var reg = new RegExp(searchstr, 'i');
bla.search(reg); // 0
Cü,
Kai
The 2nd problem is the followin: I wanna get a new div for every one of those outputs, but i don't get. I use a for (var bla bla bla)
var kol = document.getElementById("kol"); var newdiv = document.createElement('div'); newdiv.id = 'hallo'; newdiv.innerHTML = '<div id="klasse1">Search: ' + search\_output[i]["name"]; kol.appendChild(newdiv);
And i defined a css id with "klasse1" and told it that there should be top 5px, but all those divs just still hang together.
Hello!
About the 2nd problem - try this:
var kol = document.getElementById("kol");
var newdiv = document.createElement('div');
newdiv.setAttribute("id","hallo");
kol.appendChild(newdiv);
newdiv.innerHTML = '<div id="klasse1">Search: ' + search_output[i]["name"];
First you have to append the DIV into the DOM before you can use innerHTML. Same thing with newdiv.id. This argument only works on existing elements (AFAIK), try setAttribute() to set the id.
Greetings