Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Want to make a text box input value is used as a static

Status
Not open for further replies.

gdgdgdgd

Technical User
Nov 16, 2010
4
IE
I have a text input with below code snip

<div id="related_inputs" class="related-items-actions-inner">
<label>id: </label>
<input type="text">
</div>

It does not contain any name for this text input. The above one is complete code.

I want to fix the value for this text box to 12345 so that always by default this text box will have same value if user is not specifically providing any input.

Any help how I can make input value as a static default.
 
[tt]<input type="text" [blue]value="12345"[/blue] />[/tt]

ps: Why do you choose the radio button: Helpful Tips for this question when you post?
 
Thanks Tsuji for looking my requirement:

Actually I am doing customization on already existing page which is hard coded with this text input. I am not permitted to make the change in the existing page.

For my special requirement I want to make this text input value by default static with 1234 in case users does not provide any value. For this i have created a button in the corner of the page and want to use this text input with my button with an existing function.

But when I am unable to get default value of this text input. any help?







 
You cannot change the existing page. But you can create a button in the corner of the page. If that is not contraditory, what is the button and what is it handler?

"Default static": that's nice. What language are you using as the application? How does it interface with the web page?
 
I mean to say ...I am using the grecey monkey script which supports java script and its and add on for fire fox.

Using java script i will create a button and will call a method say as Fun1(). Fun1 will take the static value of the text box which i want to be static default and will update the page with some message.

But as text input does not contain any name or ID i am unable to use it in fun 1 using getelementbyid or getelementbytagname .

Sorry for making bit contradictory...hope u understand my requirement.
 
Hi

The sweetest in the GreaseMonkey scripts is that they not need to be portable, so you can use the latest features implemented in your FireFox, for example the Selectors API or XPath.

For example this will set the [tt]value[/tt] of the [tt]input[/tt] who's parent is a [tt]div[/tt] with [tt]id[/tt] related_inputs and is preceded by a [tt]label[/tt] :
JavaScript:
document[teal].[/teal][COLOR=darkgoldenrod]querySelector[/color][teal]([/teal][green][i]'div#related_inputs>label+input'[/i][/green][teal]).[/teal]value[teal]=[/teal][green][i]'12345'[/i][/green]
For more, take a look at [tt]document.querySelector()[/tt]/[tt]document.querySelectorAll()[/tt] or the [tt]document.evaluate()[/tt].


Feherke.
 
Within the common denominator of javascript support, you can do it like this with full identification of the input element (using all the info avail, it can be less if the author is satisfied the adequacy of it) on button click or onload.
[tt]
function handle() {
var cinput=document.getElementsByTagName('input');
for (var i=0; i<cinput.length;i++) {
var o=cinput;
if (o.type.toLowerCase()=='text' && o.parentNode.id=='related_inputs' && o.parentNode.className=='related-items-actions-inner') {
o.value=(o.value.replace(/^\s+|\s+$/,''))?o.value:'1234';
}
}
}[/tt]
 
Hi Tsuji,

When I am using your function nothing happening at the click event of my button when I am calling this function on click event.

I am pasting my complete code which I used and it did not worked.Any help...?

// ==UserScript==
// @name Xen Relate
// @description RCR Infrastucture
// @include // ==/UserScript==

var ss = document.createElement('script');
ss.type='text/javascript';
var UpScr = "function updateTicket() { document.getElementById('correspondence').value='Related to Xen Parent Issue ..Refer related TT'; document.forms.form_ticket.submit()}";
var tt = document.createTextNode(UpScr);
ss.appendChild(tt);
var hh = document.getElementsByTagName('body')[0];
hh.appendChild(ss);


function handle()
{
var cinput=document.getElementsByTagName('input');
for (var i=0; i<cinput.length;i++)
{
var o=cinput;
if (o.type.toLowerCase()=='text' && o.parentNode.id=='related_inputs' && o.parentNode.className=='related-items-actions-inner')
{
o.value=(o.value.replace(/^\s+|\s+$/,''))?o.value:'0008186533';
}
}
}

var Updatett = document.getElementById("header");
var UpdateButton = document.createElement("input");
UpdateButton.style.fontWeight = 'bold';
UpdateButton.value="XenR";
UpdateButton.setAttribute("onClick","handle()");
UpdateButton.setAttribute("type","button");
Updatett.appendChild(UpdateButton);


also uploaded the same
 
>UpdateButton.setAttribute("onClick","handle()");
[tt]UpdateButton.[red]onclick=handle[/red];[/tt]

The rest, I don't see anything like the original post.
 
Hi

>UpdateButton.setAttribute("onClick","handle()");
UpdateButton.onclick=handle;
According to my GreaseMonkey experience, assigning a function to [tt]onclick[/tt] does not work. But again, fortunately GreaseMonkey not needs to be compatible with anything else than Gecko, so just use the standard way to add the event handler :
Code:
UpdateButton3[teal].[/teal][COLOR=darkgoldenrod]addEventListener[/color][teal]([/teal][green][i]'click'[/i][/green][teal],[/teal]handle[teal],[/teal][b]false[/b][teal]);[/teal]

Feherke.
 
or, if you like.
[tt]
if (UpdateButton.attachEvent) {
UpdateButton.attachEvent("onclick",handle);
} else if (UpdateButton.addEventListener) {
UpdateButton.addEventListener("click",handle,false);
}
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top