Using a variable inside a input check in JS - javascript

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.

Related

JS onClick not working

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.

callback still doesn't work

I wrote post several hours ago, where I have several interesting answers, but I don't uderstand everything and now I can't use that post. So I create one new.
I sill have problem with callback in this code:
<div id='temp'></div>
<div id='points'></div>
<div onClick ="play(1, 1)" id='click'>clickclickclick</div>
<script>
play(function (addpoints) {
document.getElementById('temp').innerHTML = addpoints;
}());
function play (choosecolor, randescolor) {
if (choosecolor == randescolor) {
if (document.getElementById('points').innerHTML === '') {
document.getElementById('points').innerHTML=100;
}
else {
var points = function (allpoints) {
var addpoints = parseInt(allpoints) + 100;
callback(addpoints);
document.getElementById('points').innerHTML=addpoints;
}
}
points(document.getElementById('points').innerHTML);
}
}
I uderstand that function callback doesn't send var addpoint with its value to second function, so in second function addpoints is undefined. But I don't understand why addpoints lose its value (but I can show it in div), what callback do and how correct it. Some user gave me advise that I should to choosecolor(addpoints); instead of callback(addpoints); but it still not work.
After some thoughts and research. i came accross that i was wrong somewhere. Here is the solution , pass the last parameter - which is the name of the function you want to call back. Now this will work as you want it to work.
Now here i mentioned the third parameter as name of the function i want to call after this function.
<div id='temp'></div>
<div id='points'></div>
<div onClick ="play(1, 1, newcallback)" id='click'>clickclickclick</div>
Here i declared a newcallback function which i will call after this method:
function newcallback(addpoints) {
document.getElementById('temp').innerHTML = addpoints; //undefined
});
function play (choosecolor, randescolor,callback) {
if (choosecolor == randescolor) {
if (document.getElementById('points').innerHTML === '') {
document.getElementById('points').innerHTML=100;
}
else {
var points = function (allpoints) {
var addpoints = parseInt(allpoints) + 100;
callback(addpoints);
document.getElementById('points').innerHTML=addpoints;
}
}
points(document.getElementById('points').innerHTML);
}
}
Here is the fiddle : JsFiddle

how to call a jquery / java script variable acuretly

i have #cross_1 , #cross_2 , #cross_3 , #cross_4
and every #cross_id have each #id_green
not showing any result nor any error...
var setId = 2;
var defaultTime = 3000;
$(document).ready(function () {
setLight(setId,defaultTime);
});
function setLight(setId,defaultTime) {
//deactivateAll();
activeGreen(setId,defaultTime);
}
function deactivateAll() {
$('#cross_1 #id_red').addClass('red');
$('#cross_2 #id_red').addClass('red');
$('#cross_3 #id_red').addClass('red');
$('#cross_4 #id_red').addClass('red');
}
function activeGreen(setId,defaultTime) {
alert('#cross_'+setId+ '#id_green');
$('#cross_'+setId+ '#id_green').addClass('green');
}
function activeYellow() {
//$('#cross_'+setId+ ',#id_yellow').addClass('yellow');
}
put a comma between each selector
$('#cross_'+setId+ ',#id_green').addClass('green');
I think its just your space in the jQuery selector that is in the wrong place
function activeGreen(setId,defaultTime) {
$('#cross_'+setId+' #id_green').addClass('green');
}
If your structure is:
#cross_1
#id_green
#cross_2
#id_green
And so on, which is invalid html as mentioned by others

Is it possible to bind to the Click event and not use an anonymous function -- I just want to call a named function

I have the following code. The first attempt at binding to click event does not work. The second way does. The first shows the alert "CheckBox1" during Page_Load. The second one shows the alert "CheckBox4" during the proper time -- during clicks.
$(document).ready(function () {
$(document.getElementById(checkBox1ID)).click( SetCheckBox1State(this.checked) );
$(document.getElementById(checkBox4ID)).click(function () { SetCheckBox4State(this.checked) });
});
function SetCheckBox1State(checked) {
alert("CheckBox2");
var radNumericTextBox1 = $find(radNumericTextBox1ID);
var wrapperElement = $get(radNumericTextBox1._wrapperElementID);
var label = $(wrapperElemenet.getElementsByTagName("label")[0]);
if (checked) {
radNumericTextBox1.enable();
label.addClass("LabelEnabled");
label.removeClass("LabelDisabled");
}
else {
radNumericTextBox1.disable();
label.addClass("LabelDisabled");
label.removeClass("LabelEnabled");
}
}
function SetCheckBox4State(checked) {
alert("CheckBox4");
var radNumericTextBox2 = $find(radNumericTextBox2ID);
var wrapperElement = $get(radNumericTextBox2._wrapperElementID);
var label = $(wrapperElemenet.getElementsByTagName("label")[0]);
if (checked) {
radNumericTextBox2.enable();
label.addClass("LabelEnabled");
label.removeClass("LabelDisabled");
}
else {
radNumericTextBox2.disable();
label.addClass("LabelDisabled");
label.removeClass("LabelEnabled");
}
}
Am I doing something improper? I'd rather not use an anonymous function...but maybe this just how things work?
This code:
.click( SetCheckBox1State(this.checked) );
Assigns the .click() function to be the output of this function: SetCheckBox1State(this.checked).
You will have to get rid of the argument (make it internal) and just pass the function name:
.click( SetCheckBox1State );

write css property pbackground in js ,cant work in firefox

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)";
}
}

Categories