I have this js code
elem=$(this);
var tester_names="";
tester_names=$("#release_tester_tokens").val();
alert(tester_names)
if (elem.attr('checked')==true)
**tester_names=tester_names+", "+ elem.attr("title");**
alert(elem.attr("title"))
alert(tester_names)
I want to have tester_names=tester_names+", "+ elem.attr("title"); to have the combination of testers_names (a,b,c,d,e) and elem.attr("title") (f) to become (a,b,c,d,e,f)
The alerts that I used is for debugging to see what values are stored in the variable.. They all store correctly, but they don't combine together when I call the bolded function... I just want to know why. I am using formtastic textarea instead of the normal textbox... do I have to adjust to that? Or maybe what the tester_names and elem.attr are outputting are of different type?
I tried it using this type of code (this is nearly the same with different variable names
function updateTextArea() {
var allVals = [];
$('.taglist :checked').each(function(i) {
allVals.push( $(this).val());
});
$('#video0_tags').val(allVals).attr('rows',allVals.length) ;
}
$(function() {
$('.taglist input').click(updateTextArea);
updateTextArea();
});
});
why does this add to checkbox values to the textarea perfectly whenever I check checkboxes when mine just outputs same results before and after using the starred function?
(I don't understand why ppl keep voting this down... its seems like a decent question after the first mishap and fix) :S
Use elem.attr('checked')=='checked' in if statement. There is no case elem.attr('checked')==true will be true. So the body of your if , will never be executed.
Related
Ok hello guys here is my problem i was try to create a jPrompt but i can't understand how can i make a simple if ckeck. I find the code here and i crate it again.Now i want to do something simple, the user click the button and must enter a code for example number 123, and i must check if the number is 123(true) then run an sql query else run this function again unless the user click cancel button and not ok.
I need a good example to learn how t use if statement with Jquery, any simple idea beginner in (jQuery) sorry!!!.
This is what i have try
$("#prompt_button").click( function(e)
{
e.preventDefault();
/*
jPrompt is function which will show custom promt window.
It has three argument.
First argument is label text.
Second is the predefined value for promt.
Third is promt heading.
and has callback function which will perform exatra
code like what user enter.
*/
jPrompt('Type something:', 'Prefilled value', 'Prompt Dialog', function(r)
{
if(r!=123){jPrompt('Enter the right code plese: ');}
else jAlert('Confirmed: ' + r, 'Confirmation Results');
});
});
I don't really understand your problem. Can you supply any code?
I need a good example to learn how t use if statement with Jquery, any
simple idea beginner in (jQuery) sorry!!!.
An "if" statement is fundamental to any client/server language.(Maybe different syntax in some)
var i = 123;
$('.selector').click(function()
{
if(i == 123)
{
alert("Value is " + i);
}
});
Pretty basic click event with an "if" statement. It maybe a good idea to hit a book store or pluralsight.
//Edits
Please remember when creating the later part of an "if / else" statement it needs to be wrapped in curly braces "{}"
Regards,
Sorry guys the answer it was easy to find after 2-3 hours i was searching, and (now i will hit my head to the monitor in front of me), here is the code
$("#prompt_button").click( function(e)
{
e.preventDefault();
/*
jPrompt is function which will show custom promt window.
It has three argument.
First argument is label text.
Second is the predefined value for promt.
Third is promt heading.
and has callback function which will perform exatra
code like what user enter.
*/
jPrompt('Type something:', 'Prefilled value', 'Prompt Dialog', function(r)
{
if( r!=123 ) {$("#prompt_button").click();}
else{window.location.href = "http://stackoverflow.com"}
});
});
It's nice to loose 2-3 hours for something like that, it was simple just call, with the right way, the function (Programming for Dummies( <-This is for Me ). Thanks anyway guys.
I have a small app with one form and one input field. When a user submits this form, I first want to see if the value only contains letters. If all is good, I want to pass the value on to a function.
Here's what I have:
$('form').on('submit', function(e) {
if ($('input').val().match(/^[a-zA-Z]+$/)) {
someFunction($('input').val());
} else {
// Error message or something else here
}
e.preventDefault();
});
I don't like writing $('input').val() twice (once in the conditional statement, and again if it holds true). Using this wouldn't work, since it's within a conditional statement and not some sort of function... Is there a way to not repeat code in this scenario?
Perhaps setting $('input').val() to a variable would be best?
Thanks!
Just do this:
var inputValue = $('input').val();
Bit old but I found this helpful : Not repeating selectors
var myvar = $('input');
As well as the clear discription :
basically every time you use $(someselector) you iterate through the dom. If you can you should store the element reference
I am very close to finishing this program but am unable to get past one last hurdle. I want some very simple code to execute when the command buttons are pressed. When the Submit Order button is pressed the following code should run to check that the form is completed.
function validateForm()
{
if ($("tax").value = 0)
{
alert ("You have not selected anything to order");
}
if ($("shipCost").value = 0)
{
alert("You must select a method of shipping");
}
}
And when the reset button is pressed the following code should run.
function initForm()
{
$('date').value = todayTxt();
$('qty1').focus();
}
Unfortunately the buttons are not executing the code which I am trying to execute through the following set of functions.
window.onload = function ()
{
initForm();
todayTxt();
productCosts();
shipExpense();
$('shipping').onchange = calcShipping;
calcShipping();
$("Submit Order").onclick = validateForm();
$("reset").onclick = initForm();
}
I have created a fiddle so you can see the full program: http://jsfiddle.net/KhfQ2/ Any help is greatly appreciated.
You're doing it way wrong.
With if statements, you use == instead of =.
= in A = B means assign value of B to A
== in A == B means A equals B
Read about .ready and use it instead of window.onLoad, it's quite a bad choice when it comes to binding, ie.
$( document ).ready(function() {
//taken from api.jquery.com/ready/
});
If you're using jQuery, use # when refering to ID objects, ie.
$('#tax').val();
On no account should you use spaces when giving any object a unique name or class!
Pay attention to letters. You had ".clisk()" instead of "click()".
Check it out and provide us with fixed code.
It is simple. $("Submit Order") doesn't work, because the button doesn't have this id. You can change this to something like $("btn-submit-order"). Same thing to reset.
Moreover, when you test $("tax").value = 0 I think you mistyped = instead of ==.
Other issues...
I think you mean
if ($("#tax").val() == 0)
Note:
Uses the correct selector #
Uses the jQuery val() function. The jQuery object doesn't have a value property.
Compares to 0 using loose checking, though personally I would write the line as
if (+$("#tax").val() === 0)
So I'm trying to make this smooth color fade transition for my website. I made a function that should do just that, and it even looks like it would work, but I am having a problem I do not understand in the least, nor have I even seen anything like it before. I am using recursion in my function and passing a color I changed back into the function to be changed again. I only exit when the current color matches the target color. What I have COMPLETELY CONFIRMED is happening is that when I pass the 2 arrays back into the function again, they split themselves apart.
Ex: before being passed through, startColsAr=[211, 211, 211].
after being passed through, startColsAr=['2'; '1'; '1'; ','; '2'; '1'; '1'; ','; '2'; '1'; '1'].(semi-colons were used to make reading easier).
As you can see in my function below, nowhere am I doing anything to change the contents of the arrays like that.
Here is my function:
function transitionOut(startColAr,endColAr, waitTime, interval, page){
//change the colors
for(var i=0;i<3;i++){
if(startColAr[i]<endColAr[i]){
startColAr[i]+=interval;
if(startColAr[i]>endColAr[i]){startColAr[i]=endColAr[i];}
}else if(startColAr[i]>endColAr[i]){
startColAr[i]-=interval;
if(startColAr[i]<endColAr[i]){startColAr[i]=endColAr[i];}
}
}
//var color="rgb("+startColAr[0]+", "+startColAr[1]+", "+startColAr[2]+")";
var color = "#"+startColAr[0].toString(16)+startColAr[1].toString(16)+startColAr[2].toString(16);
document.body.style.backgroundColor = color;
if(startColAr[0]==endColAr[0] && startColAr[1]==endColAr[1] && startColAr[2]==endColAr[2]){
location.href=page;
}
window.setTimeout("transitionOut(\'"+startColAr+"\', \'"+endColAr+"\', \'"+delay+"\', \'"+interval+"\', \'"+page+"\')",waitTime);
}
Now, I am only passing in 3 values for both arrays, so I could easily change the 2 arrays to 6 integer parameters(and probably get the function to work(so don't tell me to go download a new library for transitions, THIS IS ABOUT ARRAYS)), but I want to know why this is happening. Does anyone have any suggestions? I have tried a bunch of different things to narrow it down, but I can't seem to put a dent in this at all. A little help?
The problem is with the line:
window.setTimeout("transitionOut(\'"+startColAr+"\', \'"+endColAr+"\', \'"+delay+"\', \'"+interval+"\', \'"+page+"\')",waitTime);
You are converting your arrays into strings when you are preparing the code to be run later. As per the Mozilla doc, it would probably be easier to use the alternate form of the setTimeout function:
window.setTimeout(transitionOut, waitTime, startColAr, endColAr, waitTime, interval, page);
I think this line is your problem
window.setTimeout("transitionOut(\'"+startColAr+"\', \'"+endColAr+"\', \'"+delay+"\', \'"+interval+"\', \'"+page+"\')",waitTime);
You probably want something like
window.setTimeout(transitionOut, waitTime, startColAr, endColAr, waitTime, interval,page,waitTime);
You can call setTimeout with a string literal but I don't think it's recommended.
I'm working on an ASP.Net webpage which will use the jQuery Dropdown Checklist (http://code.google.com/p/dropdown-check-list/). I'm fairly inexperienced with JavaScript and completely new to jQuery.
What I want to do is collect the values of the selected items every time a checkbox is checked/unchecked.
Here's what I have so far:
var values = "";
$("#s1").change(function () {
$("#s1").dropdownchecklist(function(selector) {
for (i = 0; i < selector.options.length; i++) {
if (selector.options[i].selected && (selector.options[i].value != "")) {
if (values != "") values += ",";
values += selector.options[i].value;
}
}
});
});
I think the problem is in the 3rd line, but I'm not sure exactly what is wrong.
If anyone could point me in the right direction, I'd appreciate it. Thanks in advance.
Response to zod...
Thanks that was pretty much exactly what I needed. However, I still have some kind of syntactic error. Here's my code:
var values = "";
$("#s1").change(function () {
$("#s1 option:selected").each(function () {
if (values != "") values += ",";
values += $(this).value();
});
alert(values);
});
When I run it, the jQuery Dropdown Checklist looks like a regular list, so I must still have something wrong.
This is somewhat off-topic, but are there any tools that make working with JavaScript and jQuery any easier? I've been spoiled working with Visual Studio and using its debugger and intellisense. Is there anything like that for JavaScript and jQuery?
Response to Ender...
Wow. I like your solution. Yeah, I tend to over-complicate things.
I'm not sure what I did wrong when I tried zod's solution, it probably also works, but I think I'll go with the simpler one.
Thanks for you help.
You don't need to get anywhere near as fancy as you are. Inside your .change() event, simply access the .val() of the select to get the values of the checked items. Like this:
$(function() {
$("#s1").dropdownchecklist();
$('#s1').change(function() {
alert($(this).val());
});
});
See a live demo here: http://jsfiddle.net/Ender/ZsMc4/
Did you try .change()
check this
http://api.jquery.com/change/
http://www.texotela.co.uk/code/jquery/select/