Click here to Skip to main content
15,885,920 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I got this jquery function

$('#btnMoreEnquiriesSELink').click(function() {
$.colorbox.close();
setTimeout(function(){window.location.href="enquiry.php";}, 500);
$( "#h2" ).attr( "checked", "checked" );
});

When btnMoreEnquiriesSELink is click,it should go to enquiry.php page and Checkbox for the SELink will automatically tick.

How can i achieve that?the jquery is in project.html page while the SELink Checkbox code in enquiry.php. The code make by others and last time all the code in one page.

Update Try Solution

XML
<script type="text/javascript" src="js/checkbox.js"></script> - I put this on both page (Project and Enquiry)


Checkbox.js Code
<script>
function callEnquiry() {
    $.colorbox.close();
    setTimeout(function(){window.location.href="enquiry.php";}, 500);
    $( "#h2" ).attr( "checked", "checked" );
}
</script>

Project Page Code

<div class="btnMPPopUpLeft">&nbsp;</div> <div class="btnMPPopUpRight">
<a href="#" id="btnMoreEnquiriesAPLink" ><div class="btnMoreEnquiries">FOR MORE ENQUIRIES</div></a></div>



<script type="text/javascript">
$('#btnMoreEnquiriesSELink').click(callEnquiry);
</script>


Enquiry Code
XML
<div class="checkbox">
<input type="checkbox" name="type[]" value="content-text" id="h2" class="checkbox" 
<?php 
for($i = 0; $i<count(@$_POST["type"]); $i++)
{
$jyef = $_POST["type"][$i];
if($jyef == 'content-text'){
echo "CHECKED";
}else{
echo "";
}
}
?>
/>
<label for="h2" class="checkboxLabel" >content-text</label>
</div><br class="clear">


P/s : i try to change attr( "checked", "true" ); but still not working
Posted
Updated 1-Oct-14 18:50pm
v3

1 solution

You have to copy the function from project page to js file and link it both in your project page and enquiry.php. If you cannot change project page then you have to make a copy that will be available elsewhere.

In this case you should also refactor so that your click doesn't call anonimous function, set the code within to separate
JavaScript
function callEnquiry() {
    $.colorbox.close();
console.log("setting location");
    window.location.href="enquiry.php"; // you can set location immediately
// and try changing checked only when it loads (assuming it loads under 500ms)
console.log("waiting for h2");
    setTimeout(function(){$( "#h2" ).attr( "checked", "true" ); 
                    console.log("h2 set to checked");}, 500);
console.log("done");
} 


and on click just call callEnquiry

JavaScript
$('#btnMoreEnquiriesSELink').click(callEnquiry);



But this doesn't look like good design, binding two pages so closely. Especially since you depend on enquiry loading within fixed time. Better would be to send (via query parameter?) some flag that will check if #h2 needs to be checked.

Something like this:
JavaScript
$(".btnMPPopUpLeft").on("load", "#h2", function() {
$( "#h2" ).attr( "checked", "checked" );
});



Finally, last two options
- if you have to set it always, why not just have checked="true" in HTML?
- I'm no PHP expert, but it seems to be that your PHP should set checked...not outside javascript...if I read your code correctly, you loop through number of elements which set either "CHECKED" or "" for the condition...but you never return...shouldn't that result in the control like this:

HTML
<input type="checkbox" name="type[]" value="content-text" id="h2" class="checkbox" CHECKBOXCHECKBOX
/> 
for two content-text controls?
Also note that you're not closing the div.

Please check the html of your page WITHOUT javascript function. Also, use developer tools in the browser (even IE has some) and check console to see if your function gets called. Finally,in your function you can set console.log("message") ; to see what gets called...
 
Share this answer
 
v5
Comments
Member 11082511 1-Oct-14 5:16am    
Hi Sinisa Hajnal..Thank for you solution. I already try both the solution but still not get it working. The checkbox still not checked automatically. Anyway thank and really appreciate it.
Sinisa Hajnal 1-Oct-14 6:14am    
try attr("checked", "true")

Also, use developer tools and check console log - if there is some jscript error (null values etc) it will be shown there.

Can I see the code (use Improve Question link under your question) - let me see how did you implement my solution so we may improve it.

Thank you
Member 11082511 1-Oct-14 23:41pm    
Hi Sinisa Hajnal...i try to use your second solution and fix some javascript error that show on developer console, but right now went i go to enquiry page directly the #h2 will checked even i not click it from the #btnMoreEnquiriesSELink button.
Member 11082511 1-Oct-14 7:04am    
ok tomorrow i will try to implement it and try to post my code. thank again for your help
Sinisa Hajnal 2-Oct-14 2:20am    
I've updated the solution - I overlooked that setting h2 checked is outside of timeout...it is the other way around...set the location immedately and try to set the checkbox after it loads.

But as I said in the solution - this is too dependant. Under bolded text will be another solution. Just writing....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900