0

In jquery, how can I perform an alert each time a video tag occurs in a string. Could I base something upon the each function? Let's say the string is called "getTheCurrentResults".

$(getTheCurrentResults["video"]).each(function ()
{
alert ("hello");
});
Cœur
  • 37,241
  • 25
  • 195
  • 267
Andrew Howard
  • 2,818
  • 5
  • 33
  • 59

2 Answers2

2

I don't know what type getTheCurrentResults is , but if it's a HTML , use roasted solution.

If it's not and you have a string and you want to perform an operation on each item :

$.each('27426472247'.match(/7/g),function (i,n){alert(n)})

This , for example , will trigger a function on each '7'.

Royi Namir
  • 144,742
  • 138
  • 468
  • 792
  • @AndrewJuniorHoward What if you string contains some text with word video in it (where video doesn't represent a tag) ? – A. Wolff Jul 08 '13 at 11:29
0

If i understand your question:

$('<div />').html($.trim(getTheCurrentResults)).find('video').each(function ()
{
   alert ("hello");
});

You wrap string inside a jquery object and then use transversal method on the jq object.

A. Wolff
  • 74,033
  • 9
  • 94
  • 155
  • @RoyiNamir not true if – A. Wolff Jul 08 '13 at 11:12
  • I dont understand. he said it is html . the video element is there so whats the problem finding it under the body element ? – Royi Namir Jul 08 '13 at 11:14
  • even if it's an element string `jQuery(theString)` will do just fine. – Royi Namir Jul 08 '13 at 11:17
  • @RoyiNamir Ok, you mean not using find maybe. I'm talking about case like this: http://jsfiddle.net/vPJFC/ `var getTheCurrentResults = '';` Or, please illustrate what you mean, i don't get it – A. Wolff Jul 08 '13 at 11:18
  • find search from childen and benieth. you should add `addBack` which is the new andSelf() http://jsbin.com/isexar/2/edit – Royi Namir Jul 08 '13 at 11:26
  • @RoyiNamir ya using addBack() is surely better/more readable here, thx for your feedback, i appreciate – A. Wolff Jul 08 '13 at 11:28
  • Just checked but addBack() cannot works here as it would always returns the self element even it is not a video tag: http://jsfiddle.net/qx6qC/ – A. Wolff Jul 08 '13 at 11:34
  • yeah. this can not hold all scenarious. thats why it should be a valid html and to search it under the body element. – Royi Namir Jul 08 '13 at 11:36