Is this code correct?
if(!($('textarea: name').val().length == 0)) {
alert("test");
}
I want to check if there is something written or not inside the textarea field in the form? I ask because it's not working!?
You're missing your closing parens in your if statement. Try this:
if(!( $('textarea: name').val().length == 0 ))
{alert("test");}
There may be other jQuery selector issues.
if(!($('textarea').val().length == 0)) will work if you have only one textarea element in your page. I think what you were trying to do with that :name selector was select a specific textarea based on its name, in which case you need:
$('textarea[name=yourName]')
Since a length of 0 is "falsy", you can simplify your test to using just .length:
if ($('textarea[name=foo]').val().length) {
alert(true);
} else {
alert(false);
}
Here is a jsFiddle where you can play with it.
if ($('textarea: name').val().length > 0) {
// do something if textbox has content
}
Related
I'm trying to check if the answer selected is correct, if so, then it should add + 1 to my user score.
But I don't understand why my code doesn't work. Could you please help me where the bug or did I miss something else?
function loadNextQuestion () {
var selectOption = document.querySelector('input[type=radio]:checked');
if (selectOption.val() == questions[answer]) {
++userScore;
} else if (selectOption) {
++currentQuestionNum;
loadQuestion();
loadQuestionNum();
} else {
alert("Please pick an answer.");
}
};
$("#nextButton").click(loadNextQuestion);
});
Your radio button element is not in a jQuery wrapper object, so the jQuery method val() won't work.
Try
if(selectOption.value == questions[answer]) {
...
}
instead.
The reason your code is not working is because you're comparing a Boolean (selectOption.val() [which is wrong anyway - it should be selectOption.value]) to an element of an array (questions[answer]), and then in your else if statement you're asking if selectOption is true. This code will always run.
Fixing your code only requires you to change this:
if (selectOption.val() == questions[answer]) {...}
Into something like this:
if (selectOption.attr("id") == questions[answer]) {...}
With the HTML like this:
<input type="radio" id="Chocolate-ice-cream" />
And your questions array like this:
var questions = ["Chocolate is used in what flavour of ice cream", "Chocolate-ice-cream", ...];
Hopefully what I've said makes sense, but please tell me if you don't understand anything.
I have a form with id commentform and if any logged in user visit the page a p tag gets generated under the form that with class logged-in-as. Now I am trying to check if that p exists and if not exists then do my validation which uses keyup(). Here is a small snippet...
$('form#commentform').keyup(function() {
if( ! $(this).has('p').hasClass('logged-in-as') ) {
....
} else {
......
}
}
});
Now the problem is that the if( ! $(this).has('p').hasClass('logged-in-as') ) is not returning me the expected result whether or not that specific p exists.
Can any of you guys tell me any other/better way to check this?
$('form#commentform').keyup(function() {
if($(this).find('p.logged-in-as').length == 1) {
....
} else {
......
}
}
});
You can do this to find it.
You can use
if ($('.logged-in-as', this).length)) {
But I would rather use a variable to store that state instead of relying on checking the presence of a raw tag : what if you change your HTML a little ?
Side note: Don't use overqualified selectors. $('#commentform') is faster and logically more consistent than $('form#commentform').
Check if an element witth class "xxx" exist
if( $( ".xxx" ).size() > 0 ) {
// EXISTS
}
Edit: forgot the dot ( ".xxx" )
I am wondering if there's any way to make this possible. I want to to check if the text value of an element begins with a certain letter.
This is my non working code:
if ($('title').text().substring(0, 1) === 'W') {
$('body').css('background', '#27aae2');
}
you can try like this:
if(yourString.indexOf('A') === 0) {
}
Yes this work, see code snippet below. Please note it was created based on your original post.
$(document).ready(function() {
if ($('#element').text().substring(0, 1) === 'A') {
alert('Do something useful')
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="element">A hello</div>
I have the following code block which works great:
jQuery(".archive-job_listing-layout").click(function(evt) {
evt.preventDefault();
if (!jQuery("body").hasClass('post-type-archive-job_listing'))
return;
console.log("Click " + jQuery(this).data('style'));
console.log(jQuery(window).width());
if (jQuery(this).data('style') == "grid" && jQuery(window).width() < 800) {
jQuery("ul.job_listings").css('display','block');
jQuery("table#wswp-header-row").hide().remove();
jQuery(".table_padding").hide().remove();
return;
}
layout_to_table("click");
})
});
I want to do is add another line which like:
if (!jQuery("body").hasClass('archive tax-job_listing_type'))
return;
but adding this breaks the code. I've tried using If Else, Or (||) And (&&), but nothing works.
If i substitute 'post-type-archive-job_listing' with 'archive tax-job_listing_type' the code also works fine, i just can't seem to get both of these lines of code to work at the same time.
This should work:
if(!jQuery("body").hasClass('archive tax-job_listing_type') && !jQuery("body").hasClass('post-type-archive-job_listing'))
return;
Perhaps separating with a few more parenthesis will work out for you:
if (!(jQuery("body").hasClass('post-type-archive-job_listing')) || !(jQuery("body").hasClass('archive tax-job_listing_type')))
return;
Can use is() which accepts multiple selectors. Will act like or when more than one selector is passed to it
if(!jQuery("body").is('.archive tax-job_listing_type, .post-type-archive-job_listing'))
DEMO
I have the following code and the first alert gives me an X but then the if block never fires. I'm sure its something simple I'm doing wrong...
$('.collectionofdates1>.datenumber').click(function(){
alert($(this).html());
if($(this).html() == "X"){
alert('asdf');
return false;
}
else{
$('.collectionofdates1 .datenumber').removeClass('selecteddate');
$(this).addClass('selecteddate');
}
});
2 recommendations:
1) put spaces in your selectors: $('.collectionofdates1 > .datenumber')
2) use text() when you mean text(), not html(). Also use .trim() to make sure you don't have whitespace: if($(this).text().trim() == 'X'){