This question already has answers here:
How to move cursor to end of contenteditable entity
(8 answers)
contenteditable, set caret at the end of the text (cross-browser)
(4 answers)
Closed 9 years ago.
In my project, I am trying to set the caret position always to the end of the text. I know this is default behaviour but when we add some text dynamically, then the caret position changes to starting point in Chrome and firefox (IE is fine, amazing).
Anyway to make it to work properly in chrome and firefox?
Here is the fiddle
<div id="result" contenteditable="true"></div>
<button class="click">click to add text</butto>
var result = $('#result');
$('.click').click(function () {
var preHtml = result.html();
result.html(preHtml + "hello");
result.focus();
});
I tried adding setStart and setEnd as mentioned in this link but no use.
I got the solution here thanks to Tim down :). The problem was that I was calling
placeCaretAtEnd($('#result'));
Instead of
placeCaretAtEnd(($('#result').get(0));
as mentioned by jwarzech in the comments.
Working Fiddle
Related
This question already has answers here:
jQuery selector for id starts with specific text [duplicate]
(4 answers)
Closed 5 years ago.
can i change this button click
$('input[name="submit.one-click-premium-de.x"]').click().first();
to a button that has a wild card after first words?
$('input[name="submit.one-click*"]').click().first();
or something?
When the Button Name start with submit.one-click i will click this.
All things after "one-click" is okay. So i want like a wildcard there...
My click on top works but i want to have a start with one-click button...
I tried much strange things but thats not working
^= is the startsWith selector:
$('input[name^="submit.one-click"]').click().first();
https://api.jquery.com/attribute-starts-with-selector/
You can use ^= to do this. It works this way:
$('input[name^="submit.one-click"]').click().first();
This question already has answers here:
Multiple Spaces Between Words in HTML without
(5 answers)
Closed 6 years ago.
I'm working on a script, that should fill a table with some string data.
Using jQuery's append and text function works fine for data without a leading blank symbol.
But if the string starts with one or more blank symbols they are ignored.
Please have a look at this code example: https://jsfiddle.net/e56eb0zx/2/
var blank = " blank ";
var no_blank = "no_blank";
$("tbody").append(
$("<tr>")
.append($("<td>").text("|"+blank+"|"))
.append($("<td>").text(blank)) // why isn't the blank displayed?
);
$("tbody").append(
$("<tr>")
.append($("<td>").text("|"+no_blank+"|"))
.append($("<td>").text(no_blank))
);
How can i fix this behaviour?
Thanks in advance!
jQuery is not the problem. The generated markup contains the blank before and after the word blank. Its a problem with rendering the table.
Set the CSS-Attribute white-space to pre or pre-wrap for your table cells to get the desired behaviour.
See this edited fiddle.
This question already has answers here:
why javascript this.style[property] return an empty string? [duplicate]
(2 answers)
Closed 6 years ago.
I've been trying to get a fade in and out for a background image for a site, and I have been trying to get the background color of a div into a variable, this is what I've tried:
elem = document.getElementById('nav');
bgColor = elem.style.backgroundColor;
But once I try to alert the variable bgColor like: alert(bgColor) all I get alerted is empty in the text box. I've looked around on some Stack questions and I've tried everything that is told there and it doesn't seem to put the physical color into a variable.
window.getComputedStyle is the solution. Otherwise you'll get the "direct" styles that are empty.
This question already has answers here:
Get selected element's outer HTML
(30 answers)
Closed 9 years ago.
I want to be able to get ANY element's html code when I click on it.
So far I can only get text elements but I can't get image elements.
Here's what I have :
$(document).click(function(event) {
alert($(event.target).html());
});
this doesn't work with images tags like
<img src="" alt=""/>
but only with text ..
Can you please tell me how to proceed to get the images element ?
Thanks in advance.
I think you need to use outerHTML as fields like image/input(self closing) does not contain html
$(document).click(function(event) {
alert(event.target.outerHTML);
});
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