开发者

How can I check ID of a clicked element js

How can I check if an ID of a clicked element is lets say 'target'.

What I am trying to do is actually show and hide comment form on clicking in the text field and hide it when the user clicks out of the form. The problem is that if the user clicks Submit button the form hides and nothing is sent over. So I'll have to check if the submit buttons id matches the clicked element and not hide it in this case.

I am using ruby on rails remote_form_for and onblur and onfocus events now.

This is my bigger form that I am showing.

<div id="bigArea" style="display:none">
<% remote_form_for @horses do |f|%>
<%= f.text_area :description, {:onBlur=>"{$(bigArea').hide();$('smallField').show();}"} %>
<%= f.submit "Submit"%>
<% end %>
</div>

and this is the smaller form field that hides every time you click in i开发者_如何学Pythont.

<div id="smallField">
<%= text_field_tag 'sth',"Click to comment, {:onFocus=>"$('bigArea').show();$('smallField').hide();"} %>
</div>

My question is how can I disallow the form to hide when a user clicks submit button? I suppose I should check which element's id has been clicked, and if it's submit button's ID I should not hide the form. Or maybe there is some other way to do all this?

I would greatly appreciate any answers!


In javascript you can use the id attribute on an element to read its associated id take a look at the example below. So you could get the attribute, then check its ID, in the example below we are getting the id of this which would be the div that has an id of foo. Since this.id == 'foo' will return true it will show us an alert.

<div id = "foo" onclick="if(this.id == 'foo'){alert('Guess What everyone This div has an id of Foo')};">
    hello
</div>

Check out more DOM attributes you can get here: http://www.howtocreate.co.uk/tutorials/javascript/domstructure

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜