1

I'm currently trying to find a way to add an ID to an html button using a javascript injection. The html is simple and looks like this:

<button type="submit" class="submit btn primary-btn" tabindex="4">Sign in</button>

Now, as you can see, it doesn't have an ID or a name, making it a bit difficult to find using spynner in Python. What javascript could I inject to add an ID to this script? Would having javascript search for that entire string and then replace the entire string with an ID added ot it? Or is there a better way of going about it?

Frederik.L
  • 5,522
  • 2
  • 29
  • 41
Dustin
  • 6,207
  • 19
  • 61
  • 93

3 Answers3

4

To ensure html integrity I would suggest:

$('.submit.btn.primary-btn').eq(0).attr('id', 'foobar');
Frederik.L
  • 5,522
  • 2
  • 29
  • 41
  • no need for that if statement. `$('.submit.btn.primary-btn').eq(0).attr('id', 'foobar');` works fine even when there are no elements. – Chad Jan 06 '13 at 02:28
  • This one worked the best of me, the eq selector made it so simple. Thank you! :D – Dustin Jan 07 '13 at 08:49
  • im glad it helped you :-) eq is quite useful when you need to access a specific index within a selector – Frederik.L Jan 07 '13 at 08:56
1
$(".submit.btn.primary-btn").attr("id", "foobar");
Andrew Hubbs
  • 9,338
  • 9
  • 48
  • 71
0

If you really want to be sure to affect just that element, do like:

$('button[tabindex="4"]').attr('id', 'value');

By the way, it makes no much sense to add an ID at runtime: if you can refer to the element, you shouldn't need the attribute anymore.

moonwave99
  • 21,957
  • 3
  • 43
  • 64