javascript document.getelement not working [duplicate] - javascript

This question already has answers here:
Why does jQuery or a DOM method such as getElementById not find the element?
(6 answers)
Closed 8 years ago.
I am very new to javascript. I am trying to place value on one input field. But it is not working, I don't know why..
document.getElementById("c_add").value='sssssss';
in the text area with id "c_add", I supposed that value will place as "sssssss", but it is not setting any value..
Full Code:
<body>
<script type="text/javascript">
document.getElementById("c_add").value='sssssss';
</script>
<textarea name="c_add" id="c_add"></textarea>
</body>

Did you tried do like this?
<input type="text" id="mytext">
<script type="text/javascript">
var elem = document.getElementById("mytext");
elem.value = "My default value";
</script>

Related

using jquery for a button with a "." in the id [duplicate]

This question already has answers here:
How do I get jQuery to select elements with a . (period) in their ID?
(8 answers)
Closed 4 years ago.
Hi guys ive got a button with jquery like this :
<html>
<button id = "index.php">
</button>
</html>
<script>
$('#index.php');').click(function(){
//stuff
});
</script>
however it seems that the id with a "." isnt accepted by jquery - is there a way to make it work?
Try escaping the .
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id = "index.php">
</button>
<script>
$('#index\\.php').click(function(){
console.log('stuff');
});
</script>

javascript runs on w3schools.com but not on jsfiddle.net? [duplicate]

This question already has answers here:
Why this JSFiddle does not work [duplicate]
(1 answer)
jsFiddle: no connection between html and js? Can't call simple function from button? [duplicate]
(1 answer)
Closed 5 years ago.
ive used a basic example from w3schools.com to get here:
https://jsfiddle.net/02wu0v49/
function myFunction() {
var x = document.getElementById("fname").value;
document.getElementById("demo").innerHTML = "aaaaaa";
document.getElementById("fname").value = "bbbbb";
alert("lala3");
}
<body>
<p>A function is triggered when the user releases a key in the input field. The function outputs the actual key/letter that was released inside the text field.</p>
Enter your name: <input type="text" id="fname" onkeyup="myFunction()">
<p>My name is: <span id="demo"></span></p>
</body>
somehow the w3schools version works but it wont do anything on jsfiddle?
and it would be really nice to use [code][/code] or something to format code on stackoverflow...all those indents are terrible.
Change load type to No wrap in - <body>
Here is updated fiddle
Here is Docs
If you open the browser console in JS fiddle it lists the error. The HTML can't find the JS.

What is the difference between .getAttribute("name") and .name? [duplicate]

This question already has answers here:
getAttribute() versus Element object properties?
(7 answers)
Closed 5 years ago.
I have a simple web-application with an input text field in it looking like this:
<input id="txtip" type="text" value="10.1.1.50" />
The address 10.1.1.50 is a default value for an ip address. And from javascript I would read it like this:
txtip.getAttribute("value")
Now let's suppose to change it to 10.1.1.49. In google chrome the above javascript code will still return 10.1.1.50, while the expression
txtip.value
returns 10.1.1.49.
What is the difference? What is the "right way"?
var el = document.getElementById('testBox');
$(document).focusout(function () {
alert('el.value = ' + el.value);
alert('el.getAttribute("value") = ' + el.getAttribute('value'));
e.preventDefault();
});
<h2>Change value in the text box</h2>
<input id="testBox" type="text" value="original value" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
Found this on web might help you try following code type something and focusout
The difference is that element.value is real time and if a user changes let's say, a textbox input, it will reflect that, and show you the new value.
While getAttribute('value') will still show the original value="whateverWasHere" value.

C#: Using HTMLAgilityPack how to get a value inside a script tag [duplicate]

This question already has an answer here:
Parsing HTML to get script variable value
(1 answer)
Closed 7 years ago.
I am using HTMLAgilityPack. I want to get a value inside a script tag, see the code:
<div id="frmSeller" method="post" name="frmSeller">
<div class="clear"></div>
<script type="text/javascript" language="javascript">
Biz.Product.GroupItemSwitchLogic.init();
Biz.Product.GroupItemSwitcher.init({
properties:null,
availableMap:[{"info":{"item":"28-200- 286","price":95.99,"vehicle":null},"map":[]}],
selectedProperties:[]
});
</script>
</div>
From there I want to get value of "price" that is 95.99.
How can i get this kindly tell me, what type of Regex I can use....
Thankyou
You can do some string manipulation like this
s = the html code
var s = z.Split(new String[] { #"price"":" }, StringSplitOptions.None);
var price = s[1].Split(',')[0];
and now price variable has your price

How to set readonly property of a textbox in javascript [duplicate]

This question already has answers here:
Setting the Textbox read only property to true using JavaScript
(6 answers)
Closed 9 years ago.
i have a textbox whose property i need to set as readonly...
how to set that?
I tried
document.getElementbyid("txtbox").readonly=true;
document.getElementbyid("txtbox").disable=true;
document.getElementbyid("txtbox").setattribute("readonly","readonly");
all these are not working for me.
Disable is working but that is passing the control values as null to the database...again that is a problem for me..
You can set an ASP.NET textbox readonly through javascript.
Here is how:
<script type="text/javascript">
function setReadOnly(){
var textbox = document.getElementById("TextBox1");
textbox.readOnly = "readonly";//readOnly is cese-sensitive
}
<body onload=" setReadOnly ()">
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</div>
</form>
</body>
Hope it'll help you.
You have numerous issues with the code in your question. The very first example would work if you had got the names of the properties right:
document.getElementById("txtbox").readOnly = true;
Notice the uppercase letters (getElementById instead of getElementbyid and readOnly instead of readonly).
Here's a working example of the above.
As for your 2nd attempt, the property is disabled, not disable. And your 3rd attempt, the method is setAttribute, not setattribute. As you can see, JavaScript is case sensitive!
The proper way is:
document.getElementById("txtbox").setAttribute("readonly", "true");
Or for the jQuery enthusiasts:
$("#txtbox").attr("readonly", true);
<script type="text/javascript">
function loading() {
var input_text = document.getElementById("text");
input_text.setAttribute("readOnly", true);
}
Works for me well....... use camelcase with property....... readOnly not as readonly.....

Categories

Resources