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

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()

Related

Changing Text in WebPage with JavaScript [duplicate]

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";

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)

Can you invent html attributes? [duplicate]

This question already has answers here:
Custom attributes - Yea or nay?
(15 answers)
Closed 9 years ago.
I am working on a website using HTML5. I have a jQuery script that shows a custom tooltip on all elements that have the title attribute. I also have a script that makes an alert message appear when the user clicks a picture. The alert message will say what the title attribute equals. Unfortunately the tooltip script and the alert script interfere with each other.
My question is:
Can I make up an attribute?
Not exactly, but HTML 5 provides data-*.
In html5 , use data-XX to produce extra attributes.
see : http://www.w3.org/html/wg/drafts/html/master/dom.html#embedding-custom-non-visible-data-with-the-data-*-attributes
You can make an aditional attribute, just by naming it.
<img src="abc.jpg" data-opens="abc" data-test="abc" id="image" />
And access it in jQuery by typing
$("#image").attr("data-opens")..
Like:
alert($("#image").attr("data-opens") + " " + $("#image").attr("data-test"));
Edited, thanks to GCyrillus.
Specifically answering you question: you can make up attributes.
But your HTML will no longer be valid and since you are adding them just to store information, you should use de data-* form to add you own data storage for an element, plus it will still be a valid HTML

get the HTML for the selected including the selected [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Get selected element’s outer HTML
In jQuery I'm currently using this to get some HTML:
var content = $("tr#add-ingredients-over-here").html();
But, I actually need to include that tr in the selected HTML. I can't figure out how to do this. I also need to replace that with some other HTML after.
Any suggestions on how to do this?
What you are looking for is the outerHTML of the element. You can try this:
var content = $("#add-ingredients-over-here")[0].outerHTML;
This is supported by all current browsers. Firefox was the last to add support for this in Firefox 11 (March 2012); all other browsers already support this for at least 4 years now. If you must support older browsers than this stackoverflow thread has a jQuery-based implementation that works everywhere:
jQuery.fn.outerHTML = function() {
return $(this).length > 0 ? $(this).clone().wrap('<div />').parent().html() : '';
};
Another fully cross browser approach. Makes a clone of tbody, removes all but the row wanted and retrieves the html from it
var content = $("#add-ingredients-over-here")
.parent().clone().find('tr:not(#add-ingredients-over-here)').remove()
.end().html()
EDIT: WHy can't you just use clone() if you need the whole TR? A clone can be inserted in another table as a jQuery object
/*copy whole row to another table*/
$('#otherTable').append( $("#add-ingredients-over-here").clone() );
Try
$("tr#add-ingredients-over-here").parent().html();
It will retrieve the parent first, and then return its HTML content

How to get selectbox value with name [duplicate]

This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
Get form elements by name?
Here's a link i can't get it to work: http://jsfiddle.net/PwfzR/3/
Try this.
$('input[name="box"]');
have a look at attribute selectors
$("input[name='box']");
here is the fiddle http://jsfiddle.net/PwfzR/
This is more specific and matches <input type="text"> elements only:
$('input[type="text"][name="box"]');
Of course, if you don't want to restrict your matches to type="text", you can use the following code, as scunliffe mentions:
$('input[name="box"]');
Your problem is that you had input instead of select. Here, I'll post the corrected code. Tell me if this works:
$("select[name='selectbox']").change(function() {
alert('t');
});
$('input[name$="box"]').val();
See the full documentation here: http://api.jquery.com/attribute-ends-with-selector/
If you already know the name of the element just prepend # to the name to select it.
$('#box')
this will work for any named element.

Categories

Resources