convert jquery to javascript? [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I want to convert this jquery to javascript.
Please help
$(document).ready(function($){
if ( $("#search_field").val().length > 0 )
{
$('*[data-role=activerecord_sortable]').activerecord_sortable();
}
});

Here is how it could look like using addEventListener and querySelector
Note, the activerecord_sortable() is Ruby
window.addEventListener('load', function() {
if (document.querySelector('#search_field').value.length > 0 )
{
document.querySelector('*[data-role=activerecord_sortable]').activerecord_sortable();
}
})

Here's what I can come up with, I don't know what your activerecord_sortable function is doing off the top of my head:
document.addEventListener('DOMContentLoaded', function(){
var searchField = document.getElementById('search_field');
if(searchField.value) {
searchField.dataset['data-role'] = activerecord_sortable();
}
});

Related

How to check in javascript if a list contains any element? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
How to check if a list is not empty?
if (serialNumbersList.Any()) //c# expression equivalent
{
// do stuff
}
In javascript there are no lists but arrays, and you can check their length property
if(serialNumberList.length > 0) {
// Do your stuff
}

Show results of multiple prompt boxes using Javascript [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How to make multiple prompt boxes and sort their input alphabetically and then show on screen using JavaScript ?
Thanks
Sequentially
if you are talking about the almost obsolete native javascrtipt function prompt() then:
var array=[];questions=['A?','B?','C?'],current=0;
function q(question){
array.push(prompt(question,'write here')+' question number:'+current);
if(current<questions.length){
next()
}else{
array.sort()// needs a proper sort function
alert(array.join("\n"));
};
}
function next(){
q(questions[current++]);
}
next();
Demo
http://jsfiddle.net/c7GnX/

Use a method in a callback [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Question is being edited. Sorry for changes. Please don't provide answers at the moment :)
If I declare an object in javascript:
var MyObject = {
function myFunction() {
console.log('hi!');
}
}
Why do I get errors?
Wouldn't it rather be:
var MyObject = {
myFunction : function(){
console.log('hi!');
}
}
How to call the method:
MyObject.myFunction()

Validation on special codes [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a special ID in the format of A000000004..A000000150 etc and OD000000001..OD000000150. I have read the forums here and tried out the solutions (many in the form of /^([A]{1}\[0-9]{9})$/ etc.) but none of them have helped.
I hope I'm understanding your question properly, but how about this regex?
/^(A|OD)[0-9]{9}$/
To check a string against this regex, you'd use something like this:
var regex = /^(A|OD)[0-9]{9}$/;
if (regex.test(myCode)) {
// ... do something ...
}
***So the final solution to my question for people who will refer in future:
The code will be:
var regex= /^(A|OD)[0-9]{9}$/;
var myCode=document.forms["formname"]["fieldname"].value;
if(!regex.test(myCode))
{
alert(...);
return false;
}

How to find a certain class using .find() or some jQuery Syntax? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
if( hasWhiteSpace($(this).find(".nospace:input").val()) ){
$(this).find(".nospace:input").filter(function() {
return hasWhiteSpace(this.value) == true;
}).addClass("blank");
setError(".motd_register_company","There must be no space in between.");
return false;
}
There is something wrong about my code above, and the code only validate the first input, while the others not, how could I validate all the "nospace" class that I have in my form?
can you guys trim my code if there is something wrong.
Are you trying to recursively search elements for a particular classname? If so:
$('input.class_name').each(function() {
// Do you stuff to this input element
});
jQuery.each("input.nospace", function(i, el){
var self = $(el);
if(hasWhiteSpace(self.val()) self.addClass("blank");
});

Categories

Resources