function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sp13sp13 

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.

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

<div id="DivID1">
    text1
</div>
<div id="DivID2">
    text2
</div>
<style>
    .tabStyle1 {
        backgroound-color: red;
    }
    .tabStyle2 {
        backgroound-color: blue;
    }
</style>


Controller:
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>

<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
}
</script>

<apex:inputHidden value="{!a}" id="a_id"/>
<apex:inputHidden value="{!b}" id="b_id"/>
ezdhanhussainezdhanhussain
Hi Sp13, try this piece of Code. Mark this as solution if solved.
//javascript code
<script>
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
alert(value1);
alert(value2);
callcontroller(value1,value2)
}
</script>
//page code
<apex:actionFunction name="callcontroller" action="{!controllermehod}"  >
<apex:param value="" name="firstvalue"/>
<apex:param value="" name="secondvalue"/>
</apex:actionFunction>
<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;
}