I am totally new to javaScript and cannot understand why this elementary if else statement won't work. And before you mark down yes I have looked and can't find a simple answer to this simple question. Many thanks.
$(document).ready(function (){
var try = 0;
if (try == 0){
alert("yes");
} else {
alert("no");
}
});
Because try is a javascript keyword and you cannot use it.
Change it to try1 just for instance.
$(document).ready(function (){
var try1 = 0;
if (try1 == 0){
alert("yes");
} else {
alert("no");
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Related
I know I have issue in referring value to this after seeing some similar questions on this site, but not able to solve this in my context.
any one could help me to solve this , how do I refer value of splitInput to this as when I replace this with splitInput it is giving errors as splitInput is not an function
here is my code snippet of only js
<script type="text/javascript">
function styleBox(){
var userInput= document.getElementById('invite-emails').value;
var splitInput=userInput.split(",");
for(var i=0; i< splitInput.length; i++) {
$(this).append("<span style='background-color:red'>"+splitInput[i]+((i< splitInput.length-1) ? ",":"")+"</span>");
}
};
$(function(){
$(".team-btn").click(function(e) {
$('.team-intro').replaceWith($(".team-invite").show());
});
});
$("#invite-emails").keypress(function(e){
if(e.which == 32) {
styleBox();
}
});
You call styleBox without setting this reference, you need to do styleBox.apply(this)
$("#invite-emails").keypress(function(e){
if(e.which == 32) {
styleBox.apply(this);
}
});
Than inside styleBox this will be showing to #invite-emails
I am checking the value of a dropdown on load of the page. But when I debug, I see the controller is executing both if and else statements which is not supposed to be happening.
/* My code: */
$(window).load(function() {
if (($("#dashboardFormId\\:oneMenu1 option:selected").text() == 'Never') ||
($("#dashboardFormId\\:oneMenu1 option:selected").text() == 'Default')) {
$('#dashboardFormId\\:secsDropdown').hide();
} else {
$('#dashboardFormId\\:secsDropdown').show();
}
});
I dont find anything wrong in the above code. Please help me find the problem in the above code. Thanks in advance!
You can try writing this text:
$(window).load(function() {
if (($("#dashboardFormId\\:oneMenu1 option:selected").val() == 'Never') ||
($("#dashboardFormId\\:oneMenu1 option:selected").val() == 'Default')) {
$('#dashboardFormId\\:secsDropdown').hide();
} else {
$('#dashboardFormId\\:secsDropdown').show();
}
});
I want to pass variable "pageSetUp" from the on click event go through to function x and I keep going in circles.. I'm still a bit of noob when it comes to js. I'd really appreciate your help. Thanks!
$(".link").click(function() {
var pageSetUp = $(this).attr("ID");
});
function x(pageSetUp){
if(pageSetUp == page1){
//do something
}
else if(pageSetUp == page2){
//do something else
}else{
}
});
x();
you need call x with desired parameter.:
$(".link").click(function() {
var pageSetUp = $(this).attr("ID");
x(pageSetUp);
});
function x(pageSetUp){
if(pageSetUp == page1){
//do something
}
else if(pageSetUp == page2){
//do something else
}else{
}
};
I am trying to recreate the JS below with PHP. The reason is that the numbered classes and values are actually IDs pulled from a mysql database. I have an area where say a report is creating, the code below shows and hides rules for that report. Since different reports have different rules, it shows and hides rules dependent on the grouping, determined in the code below as #rule_who.
When trying to recreate the following I was trying to use while loops however it got pretty ridiculous. Is there a more efficient way in JavaScript or AJAX to show and hide divs that would be better suited to using a large number of divs? The 2,3,4, and so on shouldn't be an incrementing number as it would rely on IDs and thus some numbers will disappear over time as reports are deleted.
Any help would be appreciated. Thanks
<script>
//<![CDATA[
$(window).load(function(){
$(".2").hide();
$(".3").hide();
$(".4").hide();
$('#rule_who').on('change', function () {
if(this.value === "2"){
$(".2").show();
$(".3").hide();
$(".4").hide();
} else if(this.value === "3"){
$(".2").hide();
$(".3").show();
$(".4").hide();
} else if(this.value === "4"){
$(".2").hide();
$(".3").hide();
$(".4").show();
} else {
$(".2").hide();
$(".3").hide();
$(".4").hide();
}
});
});//]]>
</script>
EDIT: Thanks everyone for the help.
What I ended up using was the following:
<script type='text/javascript'>//<![CDATA[
$(window).load(function(){
$("#rule_who").change(function() {
$("div.specific_rules").hide();
var targetId = $(this).val();
console.log($(targetId).html());
// show the new selected one
$("#"+targetId).show();
});
});//]]>
</script>
I think you can do:
$('.' + this.value).show();
$('.' + this.value).siblings().hide();
Try this (FIDDLE EXAMPLE HERE):
$('.1, .2, .3').hide();
$(window).load(function(){
$('#rule_who').on('change', function () {
var elems = document.getElementsByTagName('div');
for (var i = 0; i < elems.length; i++) {
if (elems[i].className != this.value) {
elems[i].style.display = 'none';
} else {
elems[i].style.display = 'block';
}
}
});
});
Basically, is it possible to do something like....
Click me!
<script>
function clicked() {
if(myVar == 1) {
link="http://stackoverflow.com"
}
else if (myVar == 2) {
link="http://google.com"
}
}
</script>
This example is probably impossible due to it firing at the same time...
But is it at all possible to use variables there?
Basically, I need a link that'll bring you to two different places depending on a variable.
I suppose I could have two links, and just hide/show each one respectively depending on the variable, but I was wondering if it's possible another way?
I'm working with HTML, CSS, JavaScript, and JQuery...
Thank you!
You could do...
$('a').click(function(event) {
if (condition) {
event.preventDefault();
window.location = 'http://different-url.com';
}
});
If the condition is met, then it will take you to a different URL.
Otherwise, the link will work as expected.
If you didn't want to use jQuery, that'd be...
var anchors = document.getElementsByTagName('a');
for (var i = 0, anchorsLength; i < anchorsLength; i++) {
anchors[i].onclick = function(event) {
if (condition) {
event.preventDefault();
window.location = 'http://different-url.com';
}
}
}
You could simply use Javascript to do a redirect:
<script>
function clicked() {
if(myVar == 1) {
window.location = "http://url1.com";
}
else if (myVar == 2) {
window.location = "http://url2.com";
}
}
</script>
No it's not possible to do it the way you want.
Why don't you do this instead -
Click me!
<script>
function clicked()
{
if(myVar == 1)
{
window.location.href = "www.stackoverflow.com";
}
else if (myVar == 2)
{
window.location.href = "www.google.com";
}
}
Take a look at Mozilla Developer Center for further references about the window.location object.
you could do this:
<script> function clicked() {
if(myVar == 1) {
window.location="http://stackoverflow.com"
}
else if (myVar == 2) {
window.location="http://google.com"
} }
</script>
Try this out
Click me!
<script>
function clicked() {
if(myVar == 1) {
location.replace("http://stackoverflow.com");
}
else if (myVar == 2) {
location.replace("http://google.com");
}
}
</script>
Cheers. This will take you to the desired place based on the myVar values!
function clicked() {
var dest = "";
if(myVar)
dest = "http://stackoverflow.com";
else
dest = "http://google.com"
window.navigate(dest);
But I see everyone said about the same. Just check which of the methods works on most browsers