prepend and append string to input value - javascript

I'm trying to prepend and append to an input value some string, like this.
<input type="text" name="protezione" value="" onSubmit="http://www.amministrazioni-zucchetti.it/protezione/CM_<?php echo (this.value) ?>.php">
if I put for example mountains into the input field, the final value send to a form should be:
http://www.amministrazioni-zucchetti.it/protezione/CM_mountains.php
thanks

this.value looks like a Javascript declaration. In PHP, all variables start with $. The next problem is that $this is only used inside a PHP class. Something tells me you aren't doing this inside a class. And then, you refer to object properties using $this->value

For example:
<input type="text" name="protezione" onclick="alert('http://yoururl.com/' + this.value);">
And in your case:
<input type="text" name="protezione" onSubmit="http://www.amministrazioni-zucchetti.it/protezione/CM_' + this.value + '.php">
You're tried something with <?php, but this is the start tag for server-sided php syntax, which can't be executed or interpreted from the client - only from the server. So in this case php doesn't help. You might want to use PHP in your .php file you're sending the request to progrocess the data.

Related

Get value for CSS declaration inside HTML attribute

I am a newbie to CSS selectors and I am trying to get the value for a CSS declaration item assigned to the value attribute:
<input id="textUsername_ClientState" name="textUsername_ClientState" type="hidden" autocomplete="off" value="{"enabled":true,"emptyMessage":"","validationText":"Tom","valueAsString":"Tom","lastSetTextBoxValue":"Tom"}">
Basically I am trying to pull back one of the "Tom" values. I tried document.getElementById("textUsername_ClientState").value.style.getPropertyValue("valueAsString") but it didn't work.
Any help would be greatly appreciated.
As a newbie you have to learn a lot. You should be correct in declaring tag and its attribute
value=" { "enabled":true, "emptyMessage":"", "validationText":"Tom", "valueAsString":"Tom", "lastSetTextBoxValue":"Tom"} "
This line has some syntax error
correct form is
value='{ "enabled":"true", "emptyMessage":"", "validationText":"Tom", "valueAsString":"Tom", "lastSetTextBoxValue":"Tom"}'
Learn the difference between ' and ".
This is the format of JSON string. In this question you have to tried to access the value of valueAsString
before trying to get the value,you need to convert the json string to a javascript array by using JSON.parse() method. After parsing you can access the values individually.
I hope this helps.
Html:
<body>
<input id="textUsername_ClientState" name="textUsername_ClientState" type="hidden" autocomplete="off" value='{"enabled":"true","emptyMessage":"","validationText":"Tom","valueAsString":"Tom","lastSetTextBoxValue":"Tom"}'>
<button onclick="cool()">Cool</button>
<script>
function cool()
{
a=document.getElementById("textUsername_ClientState").value;
b=JSON.parse(a);
alert(b.valueAsString);
}
</script>
</body>

how to get hidden values in webdriver using javascript

There's a hidden input field in which I'm trying to insert a specific date value. The field originally produces a value, from which a user can select an appropriate value.
The page's source code looks like this:
<div id="change_img">
<img width="80" height="30" border="1" src="http://jntuh.ac.in/results/images/CaptchaSecurityImages.php?width=100&height=50&characters=5&code=ryyrh">
<br>
<input id="code" type="hidden" value="ryyrh" name="code">
</div>
Use WebElement's getAttribute method. In your case it will be:
WebElement hiddenInput = driver.findElement(By.id("code"));
String value = hiddenInput.getAttribute("value");
If for any reason you need to do it with javascript (your question specifically asked for js) then this code should work:
String script = "return document.getElementById('code').getAttribute('value');";
String value = ((JavascriptExecutor) driver).executeScript(script).toString();
I tested this solution in C# and it works. I am then able to parse the returned string to find and verify what I need.
http://yizeng.me/2014/04/08/get-text-from-hidden-elements-using-selenium-webdriver/
So in the example in the question you would get the innerHTML of the visible parent "change_img" element which will include the hidden element.
Solution in Python:
script = "return document.getElementById('code').getAttribute('value');";
print(driver.execute_script(script))
Solution in C#:
string script = "return document.getElementById('code').getAttribute('value');";
string value = ((IJavaScriptExecutor)driver).ExecuteScript(script).ToString();

ASP: Get the value from javascript using getElementbyId

I am trying to get value from javascript using getElementbyId but i am not getting it.
If i put,
<input type="text" id="disprice<% =pID %>" value="<%=disprice%>" name="Price" />
like this then i am getting value from java script in my text box.
But if i try to get that same thing like this,
<span class="productListPrice" id="disprice<% =pID %>">
then i am not getting the value..
Please help me if possible.
Mitesh
Your texbox needs an id.
More details http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_doc_getelementbyid
#mitesh: Here's some sample code that should help --
JavaScript:
var price = document.getElementById('Price');
ASP:
Dim iPrice
iPrice = Request.Form("Price")
HTML:
<input type="text" id="Price" name="Price" value="<%=iPrice %>">
Only input values are transmitted back to the server on a postback and the name is used (not the id). The id is primarily for doing things in JavaScript client-side.
Also, in both cases, getElementById will return the element with the given id. However, the element itself is different (a span as no inherent value). This step may require some more troubleshooting to determine "what" isn't working.

How to refer html element id specified in visualforce and pass onto javascript function?

I have apex tag that generate input text field.
<apex:page id="my_page">
<apex:inputText id="foo" id="c_txt"></apex:inputText>
</apex:page>
When someone clicks this field, I want to execute javascript.
But when I check the HTML source, this apex tag which becomes input tag has (I think) dynamically generated part.
<input type="text" size="50" value="Tue Nov 16 00:00:00 GMT 2010"
name="j_id0:j_id3:j_id4:c_txt" id="j_id0:j_id3:j_id4:c_txt">
As you can see id has junk part :(
id="j_id0:j_id3:j_id4:c_txt"
In my Javascript I'm trying to getElementById('c_txt') but this does not work of course. How to deal with this???
UPDATE
Seems like I can do this but not working...
<apex:includeScript value="{!URLFOR($Resource.datepickerjs)}"></apex:includeScript>
<apex:inputText id="foo" id="c_txt" onclick="javascript:displayDatePicker()" />
datepickerjs
var elem = getElementById('c_txt');
alert(elem);
The alert shows 'null' so something must be wrong.
Even this alert returns null...
var targetDateField = document.getElementById('{!$Component.my_page:c_txt}');
alert(targetDateField);
You can use the $Component notation in javascript, you use it like so:
var e = document.getElementById("{!$Component.ComponentId}");
One thing to be wary of though, is if your element is contained within several levels of Visualforce tags which have IDs:
<apex:pageBlock id="theBlock">
<apex:pageBlockSection id="theBlockSection">
<apex:commandLink action="{!someAction}" value="LINK!" id="theLink"/>
// snip
// in javascript you would reference this component using:
document.getElementById("{!$Component.theBlock.theSection.theLink}");
I got solution to my problem.
$Compoent global visualforce expression can only be used in visualforce code not inside of
Javascript as far as my search.
Below code works fine. It outputs the value in the inputText field to js alert message Now you can pass id attribute to the Javascript and process whatever the task needed.
Created Date: <apex:inputText id="dah" value="{!created}" size="50"
onclick="javascript:go('{!$Component.dah}')"></apex:inputText>
<script>
function go(field) {
var huh = document.getElementById(field).value;
alert(huh); //returns the string u put inside of input text field
}
</script>

Get escaped html from td and put it into input as value

I have html
<table>
<tr><td id="cell"><a href="">Google.com</a></td></tr>
</table>
<div id="to"></div>
And I have javascript
$(document).ready(function() {
var html = '<input type="text" value="'+$("#cell").html()+'" />'
$("#to").append(html);
});
I don't know why, but when executing this code I'm getting only <a href= in input. In firebug's inspector input html appears as <input type="text" a>="" >google.com<="" value="<a href=" > As you can see, $quot; are replaced with " - this is the problem.
I've tried using .text() instead of .html() - almost the same situation.
What is my mistake?
Thank you.
You need to encode the value (set via .val() in this case), not use it directly in a string, like this:
$(document).ready(function() {
var html = $('<input type="text" />').val($("#cell").html());
$("#to").append(html);
});
You can see it in a demo here. The problem is the &quote; gets decoded to " which is making your HTML look like this:
<input type="text" value="<a href="">Google.com</a>" />
You can see how that makes the browser a little crazy :)
Try this:
$(document).ready(function() {
$('<input type="text">').val($("#cell").html()).appendTo("#to");
});
Avoid building HTML from strings and variables, use the functions jQuery and the DOM give to you to assign values to attributes or change an element's text. It's safer this way, and IMHO it's more readable as well.
Try to write customize function to unescape the string
function unescape(html) {
return html.
replace(/&/gmi, '&').
replace(/"/gmi, '"').
replace(/>/gmi, '>').
replace(/</gmi, '<')
}

Categories

Resources