• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 523
  • Last Modified:

Search for a name in a div

I have this div in my html file. I want to be able to search for the name john bassey.
If the name is John bassey, I want  a confirm popup to come up. If the user clicks on ok it should take the user to a new page and if cancel, nothing should happen

<div class="ContentLeft">
 <h1 class="header">Employee profile - john bassey</h1>
</div>

How do I write this using jquery?
0
Sirdots
Asked:
Sirdots
  • 5
  • 4
1 Solution
 
Miguel OzSoftware EngineerCommented:
The jquery search
$('div.ContentLeft').has('h1.header: contains('john bassey'));
if this query has an object, then you found 'john bassey'

Check:
http://www.w3schools.com/jquery/sel_contains.asp

the confirm box code will look like:
var r=confirm("Press a button and type your mesage here");
 if (r==true)
   {
   alert("You pressed OK!");
   }
 else
   {
   alert("You pressed Cancel!");
   }
0
 
leakim971PluritechnicianCommented:
$(".ContentLeft:contains(john bassey)").css("background","green");¿
or :
$(".ContentLeft:eq(0)").filter(function() { return $(this).text().indexOf("john bassey")>=0; }).css("background","green");¿

http://jsfiddle.net/LZWRJ/1/
http://jsfiddle.net/LZWRJ/2/
0
 
leakim971PluritechnicianCommented:
dynamically :

var s = "john bassey"
//$(".ContentLeft:contains(" + s + ")").css("background","green");¿
// or 
$(".ContentLeft:eq(0)").filter(function() { return $(this).text().indexOf(s)>=0; }).css("background","green");

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
SirdotsAuthor Commented:
Thanks everyone for all your response. Here is what i want

1. search for john bassey.
2. if name john bassey is found, display a confirm popup box which says ok and cancel
3. if user clicks ok redirect to main.aspx else clear the popup box.

Am new to jquery. Am I able to store item 1 in a variable and then test if john bassey was found?
can I do a redirect to another page within jquery?
0
 
leakim971PluritechnicianCommented:
As you find an answer of your original question, please award points and close your question.
If needed create a new thread with your new question
Thanks
0
 
SirdotsAuthor Commented:
Thanks leakin971. My question has not been fully answered if you read the question very well.
0
 
SirdotsAuthor Commented:
This is not even working

var r=confirm("Press a button and type your mesage here");
 if (r==true)
   {
   alert("You pressed OK!");
   }
 else
   {
   alert("You pressed Cancel!");
   }
0
 
leakim971PluritechnicianCommented:
My bad :(
What about :
var s = "john bassey"
var n = $(".ContentLeft:contains(" + s + ")").length;
if( n>0 && confirm("Press a button and type your mesage here") )
{
      location.href = "http://www.domain.com/path/to/newpage.html";
}

Open in new window


?
0
 
SirdotsAuthor Commented:
Works like a charm. Thanks a million.
0
 
leakim971PluritechnicianCommented:
You're welcome!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now