I’m completely stumped. I have no clue why this isn’t working and it seems so simple to me. It’s simply supposed to check whether the checkbox is checked and transfer the value of the textbox. Instead this just pops up an alert at page load and then nothing after that.
$(function(){
if($("#same").is(":checked"))
{
alert("checked");
$("#name2").val($("#name1").val());
}
else
{
alert("unchecked");
$("#name2").val("");
}
});
Things I’ve tried:
if($("#same").attr("checked"))
adding == true on the end
$("#same").click($(this.toggle(...))) // this semi worked but toggled on and off improperly
The checkbox does have an id of “same” before it is asked.
What is wrong with this code? Like I said I’m stumped! Thanks 🙂
Edit 1: As requested here is an html snippet.
<div class="tkfmheader">Info 2 <span class="tkshcbx">Same as 1 <input type="checkbox" name="same" id="same" /></span></div>
A function like this is only handled once, on pageload.
If you want this alert to do something every time you click, you will need to set the function to an event: like the
click()event, or thechange()event.You could try something like: