最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Add ID to Button in Javascript - Stack Overflow

programmeradmin6浏览0评论

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?

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?

Share Improve this question edited Jan 6, 2013 at 2:17 Frederik.L 5,6202 gold badges32 silver badges41 bronze badges asked Jan 6, 2013 at 0:48 DustinDustin 6,30719 gold badges63 silver badges93 bronze badges 3
  • 1 Use jquery to select the element and then use $(selector).attr('id', 'value'); to add an ID – Jon Rubins Commented Jan 6, 2013 at 0:50
  • 1 Is there only one button on the page with the text Sign in or anything that is unique to the button? – Blake Plumb Commented Jan 6, 2013 at 0:51
  • The correct solution is to make the button uniquely identifiable by assigning it an ID at source. – Lightness Races in Orbit Commented Jan 6, 2013 at 14:28
Add a ment  | 

3 Answers 3

Reset to default 4

To ensure html integrity I would suggest:

$('.submit.btn.primary-btn').eq(0).attr('id', 'foobar');
$(".submit.btn.primary-btn").attr("id", "foobar");

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.

发布评论

评论列表(0)

  1. 暂无评论