I'm trying to load up a hidden field with a list of values from all checkboxes that are checked. Everything works fine except that the is(':checked') always returns false. Any ideas?
$('.invCheckBox').click(function () {
var cb = '';
var i = 0;
debugger;
$(".invCheckBox").each(function (index, element) {
debugger;
if ($(element).is(':checked')) {
alert($(element).attr('invNbr'));
if (i == 0) {
cb = $(element).attr('invNbr')
}
else {
cb = cb + ',' + $(element).attr('invNbr');
};
i = i + 1;
};
});
$('MainContent_txtHiddenField').text(cb);
});
Here is a fiddle for you might help :) http://jsfiddle.net/sAyfw/
$('.invCheckBox').change(function () {
$('.invCheckBox').each(function()
{
alert($(this).is(':checked'));
});
});
For whatever reason creating a CheckBox control server-side wraps the input tag in a span tag. Weird. So, I changed the code to create an HtmlInputCheckBox instead and now the jQuery works beautifully.
Dim divCheckBox As New HtmlGenericControl("div")
divCheckBox.Attributes.Add("style", "width: 95px; float: left;")
Dim chkCheckBox As New HtmlInputCheckBox
chkCheckBox.Attributes.Add("class", "invCheckBox")
chkCheckBox.Attributes.Add("invNbr", invoice.InvoiceNbr)
divCheckBox.Controls.Add(chkCheckBox)
divInvRow.Controls.Add(divCheckBox)
Thanks for the info about asp:CheckBox being wrapped in a span. My jQuery was always returning false, and I couldn't figure out why it was not working.
I used this:
console.log($(this).children(1).is(':checked'));
and that works perfectly with an asp:CheckBox.
Related
The input named alternativa-*** will have the *** changed in the PHP that comes before. I'm not using a form on PHP only a onClick statement calling the respondeQuestao function. But this code seems to not work. Someone have any suggestion.
$(document).ready(function() {
function respondeQuestao(qid,resposta) {
var alternativa = document.getElementsByName('input[name = "aternativa-"' + qid ']:checked').value;
document.getElementById("demo").innerHTML = 5 + 6;
if(alternativa==resposta) {
$("#botao-questao"+qid).hide();
};
if(alternativa!=resposta) {
};
};
})
Defining a function within the jQuery Ready statement limits the accessibility - define it outside of the jQuery Ready statement but call it when you need it.
function respondeQuestao(qid, resposta) {
var alternativa = $("INPUT[name^='alternativa-']:checked").val();
$("#demo").html(5+6);
if (alternativa == resposta) {
$("#botao-questro" + qid).hide()
} else {
//
}
}
Call the function inside jQuery:
$(function() {
respondeQuestao("id", 11);
});
I hope this helps.
Just don't know why this piece of code is not working: (onClick not working, click() is working (using console))
function click(ID)
{
if(cost[ID] <= currency[costID[ID]])
{
currency[costID[ID]] -= cost[ID];
currency[ID] += buyamout[ID];
document.getElementById(x[costID[ID]]).innerHTML = "<center>"+(Math.round(notyfication(currency[costID[ID]])*100)/100)+not+"</center>";
document.getElementById(x[gainID[ID]]).innerHTML = "<center>"+(Math.round(notyfication(currency[gainID[ID]])*100)/100)+not+"</center>";
}
}
...'<button onClick="click('+i+');">'+button+x[i]+'</button>'
this gives output <button onClick="click(0);">Make DNA</button>
and after clicking button nothing happens.
There could be a namespace conflict with your click. Use another name like button_click below
var i = 0;
var button = "Make ";
var x = [['DNA']]
document.writeln('<button onclick="button_click('+i+');" >'+(button+x[i])+'</button>');
function button_click(ID) { // notice the function name change
alert(ID);
}
Code below not working:
var i = 0;
var button = "Make ";
var x = [['DNA']]
document.writeln('<button onclick="click('+i+');" >'+(button+x[i])+'</button>');
function click(ID) { // the function name click may have been used already
alert(ID);
}
indeed onclick="click('+i+');" executes the javaScript code between the double brackets: click('+i+');: it calls the javaScript click() function, but this does not work if you declare function click() and someone else did that elsewhere in javaScript code.
if onClick is not working you can also use addEventListener will do the same job.
for e.g.
element.addEventListener('click', function() { /* do stuff here*/ }, false);
To answer your question you must do the following.
Change:
onClick="click(0)"
To:
onclick="click(0)"
That will most probably fix your problem.
I´m building a jQuery extension plugin with the following standard:
(function ($) {
var version = "1.1.0";
var active = false;
$.fn.inputPicker = function (options) {
return this.each(function () {
if ($(this)[0].tagName !== 'DIV')
throw new ReferenceError('mz.ui.dialog.dateTimePicker: Method works only on DIV types.');
/// Label
var labelObj = $("<label class='small'>Data Hora Inicial</label>");
$(this).append(labelObj);
/// Input
var inputObj = $("<input type='datetime-local' class='form-control input-sm'></input>");
$(this).append(inputObj);
})
});
};
}(jQuery));
And here is how I call it:
<div id='test'></div>
$('#test').inputPicker();
Later in code I wanna get the data that was entered in the input field, something like:
$('test').inputPicker().getInputData();
What´s the best way to accomplish that ? I´ve tried something like:
this.getInputData = function () {
return $(inputObj).val();
}
But got errors when calling the function.
Can someone help me with this ? Thanks in advance...
You could just make another method to get the input data like this using the DOM structure and class names that you added:
$.fn.getInputData = function() {
return this.eq(0).find("input.input-sm").val();
}
This would operate only on the first DOM element in the jQuery object (since it's returning only a single value).
So, after setting it up like you did:
$("#test").inputPicker();
You'd then retrieve the data like this:
var data = $("#test").getInputData();
I have a function that writes out to a cooke the value of the DIV that holds that data that I want to show, the cookie code works, the toggle code works but when the page refreshses, I can get the list of repeater elements, itterate through them, determine if the section should be hidden or not but I can't use visible, I can't use .show() or .hide(), I know this has to be easy but what am I over looking???
This is my working code for the slidetoggle that works and writes the true or false to the cooke based on the repeater title attribute:
$(document).ready(function () {
$("a.toggle").click(function () {
var inObj = $(this).parent().find('div#fader');
var inTitle = inObj.attr('title');
inObj.slideToggle('fast', function () {
docCookies.setItem(inTitle, inObj.is(':visible').toString());
});
});
});
This is the code block that I have the problem with, specifically, the .show() and the .hide() are not known methods, so I have the object in inObj[] collection, I am not sure how to cast this or deal with this in javascript.....
$(window).load(function () {
var inObj = $('div#fader');
for (var i = 0; i < inObj.length; i++) {
var objTitle = inObj[i].title;
var item = docCookies.getItem(objTitle);
if (item == "true") {
inObj[i].show();
}
else {
inObj[i].hide();
}
}
});
Use $(inObj[i]).show() and $(inObj[i]).hide().
this js funciton:
function(strIDHead) {
var leftTD = document.getElementById(strIDHead + "_left");
if (leftTD != null) {
leftTD.background = "Images/Button/button_left.gif";
}
}
can only work in ie,i want it work in firefox,how to do it?
ps:i tried
if (leftTD != null) leftTD.background = "url(/Images/Button/button_left.gif)";
cant work either...
Try this:
if (leftTD) leftTD.style.background = "url(Images/Button/button_left.gif)";
Or if you can use jQuery (whole function, not just the last line):
$('#' + strIDHead + '_list').css('background', 'url(Images/Button/button_left.gif)');
Update: Your function header is incorerct, too, unless you want an anonymous function and just forgot to copy the code where it's assigned somewhere. The correct function would looke like this:
function someFunction(strIDHead) {
var leftTD = document.getElementById(strIDHead + "_left");
if (leftTD) {
leftTD.style.background = "url(Images/Button/button_left.gif)";
}
}