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>
Related
I need to check if the value inside a span isn't 0, if not 0 I want jquery to alert me.
Here's the code:
<a href="./flash.php" class="button dark">Flash:
<span style="color:#15B64C;">0</span> </a>
So if in that span the value is different than zero I want to show a message with alert.
Thanks in advance :)
All you need to do is find the span contains 0 value in it by using :contains code as following, Try this code :
var span_val = parseInt($('span:contains(0)').text());
or (any of this)
var span_val = parseInt($('.button').find('span').text());
if(span_val !== 0)
{
alert('No');
}
else
{
alert('yes');
}
You can do something like this:
$('.button span').each(function ()
{
if($(this).text().indexOf('0') != -1)
{
alert("Yes");
}
else
{
alert ("No");
}
});
Here's a jsfiddle of the above: https://jsfiddle.net/AndrewL32/e0d8my79/19/
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 am using this simple code to filter through a search form with many text inputs and see if they have a value and then add a class.
Works perfectly in Chrome, safari and Firefox but not in IE9.
$('input[type="text"]').filter(function() {
if ($(this).val() !== '') {
$(this).addClass('used');
}
});
Please advice, thanks in advance!
EDIT
Change to each but doesn't solve the issue... Here it is with the event that triggers the function...
$(document).on('event-ajax-form-is-loaded', function() {
$('input[type="text"]').each(function() {
if ($(this).val() !== '') {
$(this).addClass('used');
}
});
});
From the limited information you shared, this is how you should be doing this:
$('input[type="text"]').filter(function() {
return $(this).val() !== '';
}).addClass('used');
.filter() is supposed to reduce a set of matched elements so its filter function should always return a bool instead of manipulating the DOM.
Edit: Based on your updated code snippet and the page link you shared in the comments, if you are using jQuery in WordPress, then its always safer to wrap the code like so:
(function($) {
/* jQuery Code using $ object */
})(jQuery);
enter code hereIn JS you can check the element value by getting their tag name
for (var i = 0; i < document.getElementsByTagName('input').length; i++){
if (document.getElementsByTagName('input')[i].value == "")
{
alert("The value of textbox at " + i + " is empty");
}
}
Working Demo
Or like what other people suggest, use a .each in JQuery
$('input[type="text"]').each(function(i){
if ($(this).val() == "") {
alert("The value of textbox at " + i + " is empty");
}
});
anohter Working Demo
If you insist to use filter and here you go
$('input[type="text"]').filter(function()
{ return $( this ).val() != ""; }).addClass("used");
Last Working Demo
and jquery filter reference
I have a selected box with 5 values. I'm trying to fadeIn inputs of what is selected in the box. For example: If input1 is selected, fade in input1 on click.
Here is what I'm trying to do:
$(document).ready(function(){
$('.btn').click(function() {
if($("#selectbox").value == 'Input1') {
$(".input1").show();
} else if($("#selectbox").value == 'Input2') {
$(".input2").show();
} else if($("#selectbox").value == 'Input3') {
$(".input3").show();
} else if($("#selectbox").value == 'Input4') {
$(".input4").show();
} else if($("#selectbox").value == 'Input5') {
$(".input5").show();
}
}
});
And here is a NOT working fiddle:
http://jsfiddle.net/rzMHJ/
Your code have two errors and that's why its not working.
$("#selectbox").value should be $("#selectbox").val()
you have not closed your click event with );
Also its much better to use switch case in this example.
Working Demo: http://jsfiddle.net/naveen/Zn2yy/
Update (based on Nick Cravers comment)
For this particular scenario you could simplify code a lot like this.
Demo: http://jsfiddle.net/nick_craver/BWacA/
There are two problems with your code that is causing it to fail.
First, replace .value with the jQuery function val().
Second, add ); to the second to last } at the end.
Here is working refactored code:
$(document).ready(function() {
$('.btn').click(function() {
var show = "." + $("#selectbox").val().toLowerCase();
$(show).fadeIn();
});
});
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
}