So i have a button created using this code.
<input type="button" value="Left Trigger" onclick="leftTrigger()"> </input>
What i want to happen is when it is pressed, and it calls the leftTrigger() function (which just creates a box and has you input a key value) then instead of saying "Left Trigger" the button changes its value to "Left Trigger" + lTrigger, (the variable that contains left Triggers input key value)
Since you don't show the context I added an ID to the input. I must be able to refer to it somehow.
whenever i try to run this:
<p style="color:black" >Click a button to change its value</p>
<!--LEFT TRIGGER-->
<input type="button" value="Left Trigger" onclick="leftTrigger()"> <!--<p id="ltrigger"></p>-->
<script>
function leftTrigger() {
var lTrigger = prompt("Assign a key to Left Trigger", "Enter a key");
if (lTrigger != null) {
document.getElementById("ltrigger").innerHTML = lTrigger;
}
}
</script>
it works fine. but when i add your code it makes the button stop working entirely. It still shows the button, just doesn't actually work. Do i need to put it towards the end of my script? and also what id do i need to replace 'foo' with?
You must put it after the variable is set. But the id needs to be for the button, not for the P as it is now. Now it would try to change the value of P, but P has no value so nothing happens.
InnerHTML won't work to change the text on the button.
Try this instead:
As a sidenote, it might be risky to use the same variabel name "lTrigger" as the unrelated ID "ltrigger". However it seems variable names are case-sensitive in javascript so it may not matter in this case.
I recall at least older versions of MSIE used to fetch the ID of an element even without using document.getElementById(), which might cause name collisions in some situations.
heres what i ended up using:
<script>
function leftTrigger() {
var lTrigger = prompt("Assign a key to Left Trigger", "Enter a key");
if (lTrigger != null) {
document.getElementById('left1').value = document.getElementById('left1').value + " " + lTrigger;
}
}
</script>
this works really good, but there is one small bug. it doesn't replace the value of lTrigger. instead it writes out to the button "Left Trigger J" and then the next time you click it, instead of replacing "J" with another value it will write something like "Left Trigger J I"
any solution for replacing it?
That works. thanks!
is there a way to get maxlength to work inside
<input type="button" value="Left Trigger" onclick="ltFunction()" id="ltId">
for some reason it doesn't work, and I only want the user to be able to input 1 key
he mentions in there that subst() isn't supported by version 3 browsers so should i find a different method?
That's very old browsers that are not in use anymore (like IE3), so I wouldn't hesitate to use substr().
what kind of text field are you proposing? It needs to be something that i can have easily accessed, and not take up large a mounts of space on the screen
ok if you guys want to see what the web app looks like right now, heres the file for it. http://doodayer.weebly.com/uploads/3/7/6/3/37639839/xapper.html
i only have one more question. how can i change the exact location of something on the screen? like if i wanted the buttons to be at x = 50, y = 25 how could i make them move to a specific spot?
Another thing, lTrigger will never be null. It the user doesn't type anything it will still contain the phrase you've put in, "Enter a key". Well, it would be null if the user deleted that message without typing anything instead of it. I'd leave that phrase out. You already have the instruction "Assign a key to Left Trigger". That's enough.
Also, if you don't want to hardcode the original value of the button in the script, if you for instance think you will change it in the future adn don't want to have to remember to do it in several places, you can store it in a variable, but you must do that before the function so the variable is set when the page loads (i.e. it will hold the original value) and not when the function runs (the new values would keep adding up). Then use that variable when you assign the new value.
how can i change the x,y value of the input button on the screen?
nevermind i figured it out and used this
#ltId {
position:relative;
left: 150px;
}
thanks for all the help guys! It was really great!
Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)