Changing Text in WebPage with JavaScript [duplicate] - javascript

This question already has answers here:
What do querySelectorAll and getElementsBy* methods return?
(12 answers)
Closed 4 years ago.
I have a problem with updating html element with javascript code. I'm trying to update html text input on google chrome console. For ex. cpuboss . com here is some html code from cpuboss
<input placeholder="find a cpu" name="query" type="text" id="product-lookup" autocomplete="off" class="">
I tried almost all code examples i found on stackoverflow but i couldn't update the text element.
document.getElementsByName('query').value = "test"
I tried .value .innerhtml .attribute etc. etc. but they doesn't work. Can someone help me why these codes not working ?

document.getElementsByName returns a NodeList, basically an array. You're gonna want the first one, like so:
document.getElementsByName('query')[0].value = "test";

Related

getElementById for specific html tag [duplicate]

This question already has answers here:
JavaScript and getElementById for multiple elements with the same ID
(13 answers)
Closed 6 years ago.
In fact, I am working on a small PHP script.On a page the PHP generates code like this :
<div id="ya">sjaka</div>
<p id="ya"><input id="color" type="text" value="text1"></input></p>
<span id="ya"><input id="color" type="text" value="text2"></input></span>
What i want is that to get the value of the input inside the span with the id ya.for that reason i am using this js code :
x = document.getElementById("ya");
color = x.querySelector("#color").value;
But it doesn't seem to work it returns the result inside the P tag.How can I achieve this ?
You should not have multiple elements on the page with the same ID.
ids should be unique.
Consider using a class instead.
Otherwise JavaScript just chooses one of them to select.

How to Run two JS functions on one Click? [duplicate]

This question already has answers here:
How to call multiple JavaScript functions in onclick event?
(14 answers)
Closed 7 years ago.
I was trying to run two JS functions in one click using this. Can anybody show me how to do it correctly?
<button onclick="getlocation" onclick="showDiv()">Try It</button>
I tried adding a ; between the two functions but it didnt work too.
"Adding ";" semicolon didn't work, if you could help.."
It works for sure, you have to do it like that: onclick="getLocation();showDiv()"
Was not relevant in this case:
The problem could be, that both functions are setting the content of your div, so the second function will overwrite the content!
Use .innerHTML += "..."; on you second function, this will append the content to the exisitng content.

How do i add content in an div with javascript? [duplicate]

This question already has answers here:
How can I change div content with JavaScript?
(6 answers)
Closed 7 years ago.
How do i change the content of an div on an website that has an id with javascript?
<div class="chat-area" id="chat-area">[Changeable]</div>
I want to know how i add content where it says "[Changeable]" with javascript so i can run the command through the console.
I'd like if you keep your explainage simple, because im still very new to html/css/javascript ;)
In very simple JavaScript terms, you can use:
document.getElementById("chat-area").innerHTML = 'New Content';
And yes, this would work in GreaseMonkey / UserScripts too!
You can use the innerHTML property to achieve your goal like so:
document.getElementById("chat-area").innerHTML = "Your new content here";

jQuery won't clear form [duplicate]

This question already has answers here:
How to reset a form using jQuery with .reset() method
(16 answers)
Closed 8 years ago.
I KNOW this question has been asked numerous times as I have used this website's solutions to no avail. I currently have this implementation:
$("input:reset").click(function () {
$('#answer_2').html('License number is not long enough');
$('#answer_1').html('');
$('#data_entry').each (function(){
this.reset();
});
});
I know the selector is correct as the two html changes (I put them in to confirm I was selector for the reset button click correctly) occur as they should.
Here is my form declaration:
<form name="data_entry" id="data_entry" method="post" action="">
The problem is I keep getting the error that there is no 'reset' function and the form is never cleared. This is my most recent attempt at following answers to this problem on stackoverflow site. Please help.
Why reset when you want to clear/empty?
this.empty();
might do the trick?
Your each is wrong and it's unnecessary.
$('#data_entry').get(0).reset();
This should work because you got the right element but $() returns a jQuery object, not a DOM element. get will grab your DOM element and then you can use reset() (which is not a jQuery function)

Jquery .html() how to get all html [duplicate]

This question already has answers here:
Get selected element's outer HTML
(30 answers)
jQuery: outer html() [duplicate]
(4 answers)
Closed 9 years ago.
I use this JQuery to create some elements:
var form = $(document.createElement("form"));
form.prop({"action":"/DoSomething", "method": "POST"});
var input = $(document.createElement("input"));
input.prop({"type": "image", "src": "someUrl.png"});
form.append(input);
I now want to get the entire html string that is generated by all that so:
<form action="/DoSomething" method="POST"><input type="image" src="someUrl.Png" /></form>
So that I can pass it through some other Javascript functions!
However, doing form.html() obviously only shows this:
<input type="image" src="someUrl.Png" />
So how do you get all the html?
EDIT
Yeah I can see that this is a duplicate, when I first wrote it, I had no idea what I was trying to achieve or the atleast the name given to what I was trying to achieve.
You can use:
form.wrap('<div />').parent().html()
to work across all browsers or
form[0].outerHTML
if you don't really care about older Firefox versions (< 11)
One solution:
Create a temporary element, then clone() and append():
$('<div>').append($('#xxx').clone()).html();
See more answers here: https://stackoverflow.com/questions/5744207/jquery-outer-html
check out this
form[0]
you need the element it self not wrapped version so just use the index 0
var outer_html = $('selector').clone().wrap('<p>').parent().html();
You can simply use the DOM element, rather than a jQuery object, and get outerHTML...
In your code it would be...
form[0].outerHTML;
http://jsfiddle.net/6ugyS/1/
Just use the containg element of form
form.parent().html()

Categories

Resources