+ Start a Discussion

how to pass javascript variable to apex variable

how can i pass a javascript variable to apex variable?
i need to use the apex variable on the if condition for list item class style.

    <li class="{!IF(a==true && b==false, 'tabStyle1', 'tabStyle2')}">
        P&amp;L Entry
    <li class="{!IF(a==false && b==true, 'tabStyle1', 'tabStyle2')}">
        P&amp;L Search

<div id="DivID1">
<div id="DivID2">
    .tabStyle1 {
        backgroound-color: red;
    .tabStyle2 {
        backgroound-color: blue;

public String a {get;set;}
public String b {get;set;}

public void showA() {
     //a = __________;   <----pass this javascript: $("#DivID1").is(":visible");
     //b = __________;   <----pass this javascript: $("#DivID2").is(":visible");

Amritesh SahuAmritesh Sahu
<!--include jQuery-->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

function populate()
 var a;
 var b;
  $("[id$='a_id']").val(a); //pass value to a from var a
  $("[id$='b_id']").val(b); //pass value to a from var b

<apex:inputHidden value="{!a}" id="a_id"/>
<apex:inputHidden value="{!b}" id="b_id"/>
Hi Sp13, try this piece of Code. Mark this as solution if solved.
//javascript code
function myjavascriptfunction(){
// here i have given the id directly. Provide the path to the text box you are getting value from by //clicking inspect Element on the text box
var value1 = document.getElementById("targetName").value;
var value2 = document.getElementById("targetId").value;
//alerts to verify values are poping up correctly
//page code
<apex:actionFunction name="callcontroller" action="{!controllermehod}"  >
<apex:param value="" name="firstvalue"/>
<apex:param value="" name="secondvalue"/>
<apex:inputText id="targetName" />
 <apex:inputtext  id="targetId" />
<apex:commandButton onclick="myjavascriptfunction();" value="Button" />

//apex code

public void controllermehod(){
string value1 = system.currentpagereference().getParameters().get('firstvalue');
string value2 = system.currentpagereference().getParameters().get('secondvalue');
// debug to check values are received or not
System.debug('value1 ::'+value1 + 'value 2 :::'+value2);
// now assign it to what ever apex variable you want
a = value1;
b = value2;