admin管理员组文章数量:1321261
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
3 Answers
Reset to default 4To 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.
本文标签: jqueryAdd ID to Button in JavascriptStack Overflow
版权声明:本文标题:jquery - Add ID to Button in Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742094935a2420488.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论