Set data to Label's text in javascript function - javascript

I set a javascript function to click event of LinkButton and want to set Label's text in this function,function call and pass data to it but after that when i want to read text of that Label from code behind,text is ""
//javascript function
function PopUpFunction(code) {
$("#<%= lblStatus.ClientID %>").text = code;
}
//set function to click event
lnk.Attributes.Add("onclick", "PopUpFunction(10);");
//Edit
I test Label (lblStatus) when i click on link button that call javascript's function,the text of label change but in code behind doesn't show this text (for example if text of label is 5 after i set new text in javascript's function again in code behind text is 5 although actual text is 10)

Instead of Label use HiddenField and change javascript's function like below
function PopUpFunction(code) {
$("#<%= lblStatus.ClientID %>").val(code);
return false;
}

It works for me. please check it out.
Code Behind: little modification
` lnk.Attributes.Add("onclick", "return PopUpFunction('10');");
Javascript code
`<script type="text/javascript">
function PopUpFunction(code) {
$("#lblStatus").text(code);
$("#lblStatus").show();
return false;
}
</script>

PopUpFunction(10) function is taking 10 as an integer datataype so your calling function does not get anything.Convert it or pass it as a string then you will get correct output

try this val() instead of text
$("#<%= lblStatus.ClientID %>").Val(code) ;
or ,Change text() instead of text
$("#<%= lblStatus.ClientID %>").text()=code

try something like this:
document.getElementById('The Control ID').InnerHTML = 'YourText';
example: Label1
document.getElementById('Label1').InnerHTML = 'Hello World!';

lnk.onclick = function(){
PopUpFunction('Some_Text');
});

try the following
function PopUpFunction(code) {
$("#<%= lblStatus.ClientID %>").val(code);
}
while getting it's value use this
string str=Request.Form(lblStatus.UniqueID);

Related

Jquery change color condition

Hi everyone I have an issue with Jquery :
I have a multiple selection and the user can select one thing and it will copy the text into an input above. I would like that the text in the multiple selection that will be copied become red if the button is clicked so did you understand? I don't know how to do condition in Jquery, here is what I have done :
$(document).ready(function(){
if ($choose = true)
{
$("ok").click(function(){
$("droite").css({"background-color":"yellow"});
}
});
});
droite is an id and no it's not working but I would like to know how it works
choose is a function here it is :
var choose = function(bouton){
var lesoptions = $('#droite').find(":selected");
//lesoptions.remove();
$('#numLot').val(lesoptions[0].text);
};
Can I have your opinion ?
thanks
$("ok") is wrong. it should be $("#ok") or $(".ok") or whatever.
compare operator is == instead =
Try to use like below,
var $Choose;
//Assign value to $Choose as like true or false
$(document).ready(function(){
if ($choose)
{
//If you have id like ok use "#" or if class use "." instead
$("#ok").click(function(){
$("#droite").css({"background-color":"yellow"});
});
}
});
Hope this helps...
You have to use . selector before class names and # before id names.
Read about selectors: jQuery Selectors
Since choose is a function so, you will have to return something and check if it returns true/false. So make your function like this:
function choose(bouton){
var something = /*your return value*/; //Put your return value here
var lesoptions = $('#droite').find(":selected");
$('#numLot').val(lesoptions[0].text);
return something; //something is what you want to be returned by function
};
If $choose is not defined while you are putting it in if condition then you will not get proper working.
If #ok is added dynamically then use delegation using .on.
You should put code like this:
$(document).ready(function(){
var $choose = choose(bouton);
if ($choose)
{
$("#ok").on("click",function(){ //Again not mentioned what is ok, still like you told I assume it id
$("#droite").css("background-color","yellow");
});
}
});

ckeditor: How to select all text inside editor on focus?

I am using ckeditor for text formating and there is default text inside editor which I want to show selected on focus at all time.
Here is my code
HTML:
<textarea id="FirstTextEditor" >Hello</textarea>
Javascript:
$(document).ready(function(){
CKEDITOR.instances.FirstTextEditor.on('focus', function () {
// What is code i want write to select all text on focus?
});
});
You can simply achieve what you want like this:
CKEDITOR.instances.["your instance here"].on( 'focus', function () { this.execCommand( 'selectAll' ); });
If you used JQuery, it would look like:
$(document).ready(function(){
$('textarea').focus(function(e){
var that = this;
setTimeout(
function() {
that.select()
}, 0);
});
});
JSFiddle - http://jsfiddle.net/s6Z82/5/
The only thing you now have to figure out is how do you get the current element from the CKEDITOR.instances.FirstTextEditor.on callback and you are done.
Why the setTimeout(.., 0)?
Cause seems in Chrome e.preventDefault() does not stop it from discarding the select and putting the cursor to the end of the line.

object HTMLInputElement without form in jquery html

I have a function
function changeval(fid) {
$('#'+fid).html('<span onclick="alert(' + fid + ');" class="link">Change</span>');
}
On first place fid appears as normal variable and affects the div I need (with id that is 'fid'), but on alert function it appears as [object HTMLInputElement]. I just don`t know what to do... I think I have tried everything...
You need to wrap the 'fid' with quotes in the html output, without quotes javascript alert the first element with 'id="fid's content"'.
View here
function changeval(fid) {
$('#'+fid).html('<span onclick="alert(\'' + fid + '\');" class="link">Change</span>');
}
I would remove the "onclick" and do it like this(using jquery 1.7.1)....
$(function() {//Document ready
$('body').on('click','.link',function(){
//Do what you want i.e...
alert($(this).parent().attr('id'));
alert($(this).parent().html());
});
});
function changeval(fid) {
$('#'+fid).html('<span class="link">Change</span>');
}
Hope it helps.

using html() to get changed content

I have a div that I need to grab the HTML contents of (so naturally I use html())...
However, they are text fields. Whenever I grab the contents of the div (that contains the text fields), it grabs the initial HTML and not any data changed by the end user...
Here is JS bin version..change the input field, run the function and you see that it will only take the initial value of the field...
Any way around this?
http://jsbin.com/oleni3/edit
http://jsbin.com/oleni3/5/edit
Try this out ^
The code:
$(document).ready(function() {
$('div#top input').change(function() {
$(this).attr('value', $(this).val());
});
$('#click').click(function() {
var _curHtml = $("div#top").html();
$("div#bottom").html(_curHtml);
});
});
Your getting the HTML of the div. You want to get the value of the input box:
$(document).ready(function() {
$('#click').click(function() {
var _curHtml = $("#data").val();
$("div#bottom").html(_curHtml);
});
});
No way to do it with html(). You'll have to get the value of each input using val(). You can use
$("input").each(function() {
doSomething($(this).val());
}
if it make life easier.
$(document).ready(function() {
$('#click').click(function() {
var _curHtml = $("#data").val();
$("div#bottom").html(_curHtml);
});
});
The easiest hack would be like this:
$("div#bottom input").val($("div#top input").val());
Get the value of the updated input and set that to the clone.
Updated link:
http://jsbin.com/oleni3/3/edit
$(document).ready(function() {
$('#data').change(function() {
$(this).attr('value', $(this).val());
})
$('#click').click(function() {
var _curHtml = $("div#top").html();
$("div#bottom").html(_curHtml);
});
});
This works.. here's the link http://jsbin.com/oleni3/2/edit
UPDATE: If you have many inputs inside the div, you can use this http://jsbin.com/oleni3/6/edit

How to update span when text is entered in form text field with jquery

I would like a span to update when a value is entered into a text field using jquery.
My form field has a text box with the name "userinput" and i have a span with the id "inputval".
Any help would be greatly appreciated.
UPDATE:
although you marked this as the correct answer, note that you should use the keyup event rather than the change event or the keydown
$(document).ready(function() {
$('input[name=userinput]').keyup(function() {
$('#inputval').text($(this).val());
});
});
Try the following, you have to call again keyup() to trigger for the last char:
$(".editable_input").keyup(function() {
var value = $(this).val();
var test = $(this).parent().find(".editable_label");
test.text(value);
}).keyup();
Try this. Be sure that you understand what is going on here.
// when the DOM is loaded:
$(document).ready(function() {
// find the input element with name == 'userinput'
// register an 'keydown' event handler
$("input[name='userinput']").keydown(function() {
// find the element with id == 'inputval'
// fill it with text that matches the input elements value
$('#inputval').text(this.value);
}
}
$(function() {
$("input[name=userinput]").keydown(
function() {
$('#inputval').text(this.value);
}
)
})

Categories