Alex
Alex

Reputation: 1733

JQuery Append Tag Without Id or Class

I have repetitive nobr tags which I can't add ID nor Class.

How do I find a specific nobr tag which contains a string of text?

<nobr>Due Date</nobr>
<nobr>Test</nobr>
<nobr>Hello</nobr>

$(document).ready(function() {
    $('<nobr>Due Date').append('<span class="ms-formvalidation" title="This is a required field." > *</span>');
});

If I simply have:

 $("nobr").append("*");

Then all nobr will be affected.

Upvotes: 0

Views: 629

Answers (5)

nanobar
nanobar

Reputation: 66355

$('nobr').get(1); // get the second one

Upvotes: 2

EnigmaMaster
EnigmaMaster

Reputation: 213

http://api.jquery.com/contains-selector/

$(document).ready(function() {
  $("nobr:contains('Due Date')").append('<span class="ms-formvalidation" title="This is a required field." > *</span>');
});

working example : http://jsfiddle.net/EnigmaMaster/FkZcY/

Upvotes: 2

thecodeparadox
thecodeparadox

Reputation: 87073

$('nobr').filter(function() {
  return $(this).text() == 'Due Date';
});

Upvotes: 4

rlemon
rlemon

Reputation: 17666

$("nobr").each(function() {
    if( $(this).text() == "hello world" ) {
       // this is the element with the text in it
    }
});

i'm no jQuery guy, there might be a better solution.

Upvotes: 1

GillesC
GillesC

Reputation: 10874

Use the containts pseudo selector like this

 $("nobr:contains(Test)").append("*");

Here is the documentation: http://api.jquery.com/contains-selector/

Upvotes: 1

Related Questions