How would our group find out if a particular string contains a certain substring? Only with the help of jquery, please.
You don't really need jQuery for such a simple thing, you can simply use the indexOf method of String objects, e.g.:
var str = "foobar";
var containsFoo = str.indexOf('foo') >= 0; // true
The indexOf method returns the character index where the first occurrence of the specified value is encountered, if not found, it returns -1.
Why use 10 characters when 100 will do?
Here's the requested jQuery plugin:
jQuery.isSubstring = function(haystack, needle) {
return haystack.indexOf(needle) !== -1;
};
Usage:
$.isSubstring("hello world", "world")); // true;
If your limited to jQuery which is just JavaScript... you can use the filter
var subgtSel = $("#jquerySelector").filter(function(i) {
// do your filter here
return $(this).attr("data-timestamp") <= subMsg.CreateDateTimeStamp;
});
the subgtSel becomes your new jQuery now with the relevant filter in the above. In the above, I am looking for all div elements that have an attribute that is less than the subMsg.CreateTimeStamp.
If your looking for a particular substring... you can do the following with jQuery right in the selector
var sel = $("#jquerySelector:contains('text')");
see http://api.jquery.com/contains-selector/
Related
I have a bunch of elements with names similar to "comp[1].Field" or "comp[3].AnotherField" where the index (1 or 3) changes. I'm trying to extract the index from the name.
Right now I'm using:
var index = $(":input:last").attr("name").match(/\[(\d+)\]/)[1];
but I don't feel like this is the best way to do this.
Any suggestions?
Thanks
What you have is actually a pretty good way to do it, but you should add some checking that ensures that match() actually returns an array (meaning the string was found) and not null, otherwise you'll get a type error.
example:
var index = $(":input:last").attr("name").match(/\[(\d+)\]/);
if (match) { index = match[1]; }
else { /* no match */ }
I'm obtaining a list of elements in classes and I'd like to filter out any class which does not contain a specific word in the innerHTML. Currently, I've managed to filter out the words however I can't seem to obtain the whole string, only part of it. Here's what I've got:
htmlData = $(htmlData).find(".Description").html("Fruit");
All this does is return "FruitFruitFruitFruitFruit..." when I'd like to obtain the whole string which the word is in. Anyone know how to do it? Thanks.
Wrap the assignment in an if, in which you're checking if the string is contained in the Text.
if($(htmlData).find(".Description").text().indexOf("Fruit") === -1){
htmlData = $(htmlData).find(".Description").html();
}
the indexOf function returns the index of where the string was found first. If it wasn't found, it returns -1.
I hope that this is what you meant..
var arr = [];
$.each($(htmlData).find(".Description"), function(_, jqElem){
var content = jqElem.html();
if (content.indexOf("Fruit") !== -1 ) {
arr.push(content);
}
})
I want to check if a page contains a specific word. And if it doesn't contain the word, it should to some action.
I only have the code for checking if the word is contained.
var bodyText = document.body.textContent || document.body.innerText;
var msg = "My word";
if (bodyText.indexOf(msg) > -1) {
setTimeout(function() {
window.location = "http://contains.word";
}, 1000);
}
But I want to change it to if it NOT contains it. Any solution?
A simple lookup of the indexOf function should provide you the answer...
http://www.w3schools.com/jsref/jsref_indexof.asp
The indexOf() method returns the position of the first occurrence of a specified value in a string.
This method returns -1 if the value to search for never occurs.
If that isn't clear, what you're looking for is if indexOf returns -1.
Right now I have a dynamic string that assigns it's values to a particular div class.
Output looks like this
<div class="12923"></div>
I want to find that 'randNumber' div, then check if it has another class 'x'
Currently what I have now doesn't work:
var randNumber = 12923
var lookingForYou = $$('.'+randNumber);
if (lookingForYou.hasClass('XCLASS')){alert('XCLASS FOUND!');}
$$ returns an Elements instance, Elements is an array-like Class
anyway since you are basically filtering, you can tell Slick that you need an element with both class:
var randNumber = 12923;
if($$('.' + randNumber +'.XCLASS').length>0){
alert('XCLASS FOUND');
}else{
//dostuff
}
or you could just use one of the Elements methods, I think .some will be your best choice here:
var randNumber = 12923
var lookingForYou = $$('.' + randNumber);
alert(lookingForYou.some(function(el){
return el.hasClass('XCLASS');
}))
EDIT:
adding some links:
A better way to use Elements on MooTools blog
in my second example I used the some method, which, by looking at the source is not overloaded, but is just the one in Array.prototype.some:
Element.js source reference
Array.some on MDN
$$ returns an array of all matching elems. Not sure if you can do a hasclass on an array. Might have to do a .each() then do it. Try $('body').getElement('.'+randNumber).hasClass('XCLASS') this way you grab 1 elem if you don't want to mess with the array.
Here:
if (lookingForYou.hasClass('XCLASS')){alert('XCLASS FOUND!');}
$$() returns an array, and hasClass() performs the check on each element of the array, returning an array of booleans. Unfortunately, when you check if (...), then the return array, even if all of the values are false, is evaluated as true because it's non-empty.
I'm trying to split a string based on a particular character and then count the number of characters within each part. Is there a way to do this?
So, I have:
html
blah
jquery
$(document).ready(function() {
$('.splitMe').each(function() {
var item = $(this).attr('title');
var characters = item.split("|");
// Here's where I get stuck...
// Tried various methods of length, but haven't been able to get it to work
// Most recent version that failed miserably...
var first = characters[0].text().length;
var second = characters[1].text().length;
alert(first+" "+second); //Yields characters[0] is not a function
});
});
You have too much jQuery in your mind:
var first = characters[0].length;
var second = characters[1].length;
characters is an array of strings, not jQuery objects. Strings don't have a .text() method, they are already text. Just access their length property.