unobtrusive-javascript

How to get current element in getElementsByClassName

Deadly 提交于 2019-11-30 05:06:01
Consider a simple JS event of document.getElementsByClassName('test')[0].onclick=function(){ document.getElementsByClassName('test')[0].innerHTML = 'New Text'; } How can I extend this code to generally work for all elements with class="test" . I mean getting the element clicked and replace its content. In fact, we need to get the node number (provided inside the bracket) from the click event. I am trying to better understand javascript in unobtrusive codes, not a practical method like jQuery. Just iterate over them: var elements = document.getElementsByClassName('test'); for (var i = 0; i <

submit a rails remote form with javascript

ぃ、小莉子 提交于 2019-11-30 04:54:43
In my rails app I have a remote form that looks something like this for example: <%= form_tag some_path, :method => :get, :id => 'my-form', :remote => true do %> <%= text_field_tag :search, params[:search], :id => 'search-field' %> <%= submit_tag 'Go' %> <% end %> Now i would like to submit this form via javascript and trigger all rails remote form callbacks. So far i have tried a few things but nothing seems to be working. Things i have tried: $('#my-form').trigger('onsubmit') $.rails.callFormSubmitBindings( $('#search-form') ) but no luck so far. Any ideas? You can just use .submit(); $("#my

MVC Twitter Bootstrap unobtrusive error handling

混江龙づ霸主 提交于 2019-11-29 19:39:05
I've been trying to get MVC Jquery unobtrusive error handling working with twitter bootstrap for some time now. Its got to the point were i'm either going to edit jquery.validate or do some hack and slash on document.ready. In order to get unobtrusive error handling to work with Bootstrap and MVC I need to make it so the 'error' class it appended to the 'control-group' class. As well as that, the 'error' class is appended to the input. I was wondering if anyone in the community has already found a solution. For example Typical bootstrap markup would be like so... <div class="control-group">

Reloading partial in an rails app

折月煮酒 提交于 2019-11-29 09:10:47
问题 I want to reload a partial every 3 seconds on a 'new'-view in my rails app. I have this in my new.html.erb <h1>Controller#new</h1> This is my static content <%= render partial: 'dynamic' %> More Static content How do I get this partial reloaded every 3 seconds? Do I have to use unobtrusive javascript for this? How can I achieve this through ujs? 回答1: Put partial in div <div class="dynamic"><%= render partial: 'dynamic' %></div> You can do it by using jquery like this $(document).ready(

Unobtrusive javascript with jquery - good 10 minute tutorial?

拈花ヽ惹草 提交于 2019-11-29 07:07:47
问题 I'm looking for a good 10 minute introduction to Unobtrusive Javascript using JQuery. I'm completely new to the concept, and I'd like to see how the event binding and such works. As some background, I'm looking to do a "remove this tag" system similar to what we have here on SO. Looking at the source, I didn't see any js, just an img tag. That makes me think there must be external js that's binding to the click event. I want to know how to do that, and I want a tutorial that shows me how! 回答1

How to get current element in getElementsByClassName

跟風遠走 提交于 2019-11-29 02:54:29
问题 Consider a simple JS event of document.getElementsByClassName('test')[0].onclick=function(){ document.getElementsByClassName('test')[0].innerHTML = 'New Text'; } How can I extend this code to generally work for all elements with class="test" . I mean getting the element clicked and replace its content. In fact, we need to get the node number (provided inside the bracket) from the click event. I am trying to better understand javascript in unobtrusive codes, not a practical method like jQuery.

JQuery: Why Unobtrusive JavaScript / Document Ready function rather than OnClick event?

帅比萌擦擦* 提交于 2019-11-28 22:02:22
I've just started looking at JQuery. I don't have any AJAX in my web application at present. My existing JavaScript in my HTML looks like: <form ...> <p>Find what? <input ...></p> <div class="ButtonArray"> <a href="AddRecord.ASP&Action=ADD">ADD</a> <a href="#" onClick="return MyFormSubmit();">FIND</a> </div> </form> This displays Buttons for [Add] and [Find]. For Find it is necessary to Submit the form, and I use MyFormSubmit() which validates the data, adds a visual effect to the page, and then POSTs the data. I could add a CLASS or ID to the for the Find button and in JQuery I could write

How to find all Siblings of currently selected object

核能气质少年 提交于 2019-11-28 18:18:46
What is the perfect way to find all nextSiblings and previousSiblings in javascript. I tried few ways but not getting accurate solution. if any element is selected, I need to get length of all next siblings excluding white-space, any spaces or line-breaks Also I don't want to use jquery for this, i specifically looking something from java script Please advice user113716 I'll assume that this takes place inside an event handler where this is a reference to the targeted element whose siblings you want to affect. If not, adjustments will be needed. var result = [], node = this.parentNode

jQuery UI Dialog instead of alert() for Rails 3 data-confirm attribute

半世苍凉 提交于 2019-11-28 17:06:40
In Rails 3, passing a :confirm parameter to link_to will populate the data-confirm attribute of the link. This will induce a JS alert() when the link is clicked. I am using the rails jQuery UJS adapter ( https://github.com/rails/jquery-ujs ). The relevant code from rails.js is: $('body').delegate('a[data-confirm], button[data-confirm], input[data-confirm]', 'click.rails', function () { var el = $(this); if (el.triggerAndReturn('confirm')) { if (!confirm(el.attr('data-confirm'))) { return false; } } }); and triggerAndReturn: function (name, data) { var event = new $.Event(name); this.trigger

How Does Rails 3's “data-method='delete'” Degrade Gracefully?

回眸只為那壹抹淺笑 提交于 2019-11-28 06:13:22
Rails 3 does some cool stuff to make Javascript unobtrusive, so they've done things like this: = link_to "Logout", user_session_path, :method => :delete ..converts to <a href="/logout" data-method="delete" rel="nofollow">Logout</a> But it just occurred to me.. When I turn off javascript the method isn't DELETE anymore, it's GET as expected. So are there plans to, or is there some way to, allow these data- attributes to degrade gracefully, so that link still is a DELETE request? The change they made in Rails 3 with these data- attributes wasn't about graceful degradation, it was about