1

how can i get only the cheque number(AA12GH56) not the other texts, here is my code:

HTML:

<p>your cheque number is :AA12GH56 <br /> your bank credit balance is :32,999</p>

Javascript:

$(document).ready(function(){
  $('p').click(function(){
    var cheque_no=$(this).text();
  });
});
Emre Erkan
  • 8,433
  • 3
  • 48
  • 53
Suresh Pattu
  • 6,083
  • 16
  • 59
  • 91

2 Answers2

5

You should use javascript's regular expressions if you can't modify the html. I recomend this page to learn: http://www.javascriptkit.com/javatutors/redev.shtml

If you can modify the html I would put the cheque number inside a span with a proper class name (or id):

<p>
  your cheque number is <span id="cheque-number">:AA12GH56</span> <br /> 
  your bank credit balance is :32,999
</p>

so the javascript would be:

$('p').click(function(){  var cheque_no=$(this).find("#cheque-number").text();  });  });
Chango
  • 6,754
  • 1
  • 28
  • 37
  • 1
    You should modify your code so that he can still click anywhere on the `

    ` and still get just the `cheque-number`.

    – Blazemonger Dec 05 '11 at 13:16
3

Either a regular expression or a precision .split is what you need. I'm a fan of the latter technique -- what it loses in brevity it makes up in clarity.

var cheque_no=$(this).text().split(':')[1].split('your')[0];
cheque_no = $.trim(cheque_no); // remove whitespace

http://jsfiddle.net/mblase75/c6Zms/

Blazemonger
  • 90,923
  • 26
  • 142
  • 180
  • @Chango I agree; I prefer your `span` approach. However, I assumed that OP couldn't modify the source HTML for some reason. – Blazemonger Dec 05 '11 at 14:42