jQuery: Check for existence of BR as first or last element, including text nodes within a paragraph
Date : March 29 2020, 07:55 AM
Does that help This is easy using universally supported DOM properties. jQuery isn't much use here. function isBr(el) {
return el && el.nodeType == 1 && el.tagName == "BR";
}
function isWhitespaceNode(node) {
return node.nodeType == 3 && /^\s*$/.test(node.data);
}
function isFirstOrLastChildBr(el) {
var first = el.firstChild;
if (isWhitespaceNode(first)) first = first.nextSibling;
if (isBr(first)) return true;
var last = el.lastChild;
if (isWhitespaceNode(last)) last = last.previousSibling;
if (isBr(last)) return true;
return false;
}
var $foo = $("#foo");
if ( isFirstOrLastChildBr($foo[0]) ) {
$foo.addClass("diog-fail brbr");
}
// All paragraphs
$("p").filter(function() {
return isFirstOrLastChildBr(this);
}).addClass("diog-fail brbr");
|
jQuery mouseenter on paragraph with bold text
Date : March 29 2020, 07:55 AM
Hope this helps I have a paragraph with a bold text inside , You have to use mouseleave event instead of mouseout.. $('.class1').mouseenter(function(){
$(this).addClass("highlight");
});
$('.class1').mouseleave(function(){
$(this).removeClass("highlight");
});
|
how to fit the original text format of a paragraph into the html paragraph element using jquery?
Date : March 29 2020, 07:55 AM
Hope that helps The original issue To make the section where your comment appears scale with content length, you need to replace the following html: <p id ="content" class="minimize more" style ="width :500px; height :100px; white-space :pre ">
<p id ="content" class="minimize more" style ="width :500px; min-height :100px; white-space :pre ">
|
jQuery - Making every even number in a paragraph bold
Date : March 29 2020, 07:55 AM
it fixes the issue You need to change for loop condition to i < numbers.length, use length property to get array size $("button").on("click", function() {
var countNumbers = Number($("input[type=number]").val());
for (var i = 0; i < countNumbers; i++) {
var rand = Math.floor(Math.random() * 15000);
$("#numbers").text($("#numbers").text() + rand + " ");
}
var numbers = $("#numbers").text().split(' ');
for (var i = 0; i < numbers.length; i++) {
//------------------------^-----------
if (Number(numbers[i]) % 2 === 0) {
numbers[i] = "<span class='bold'>" + numbers[i] + "</span>";
}
}
$("#numbers").html(numbers.join(' '));
});
.bold {
font-weight: bold
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="number" />
<button>Generate Numbers</button>
<p id="numbers"></p>
$("button").on("click", function() {
var countNumbers = Number($("input[type=number]").val());
var res = [];
// array for storing the random number
for (var i = 0; i < countNumbers; i++) {
var rand = Math.floor(Math.random() * 15000);
// generating random number
res.push(rand % 2 == 0 ? "<span class='bold'>" + rand + "</span>" : rand);
// checking even or odd and wrapping by span if needed finally pushing the string to array
}
$("#numbers").html(res.join(' '));
// joining the array value and set it as html
});
.bold {
font-weight: bold
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="number" />
<button>Generate Numbers</button>
<p id="numbers"></p>
|
setting caret (cursor) inside bold element present inside contenteditable paragraph
Date : March 29 2020, 07:55 AM
Hope this helps It's pretty much impossible to move the cursor into an empty element in a contenteditable div. However, as shay levi suggested in another post, you can insert the zero-width character ÈB into your empty element to give it an index value. Here's an example*: function insertNode(nodeName) {
var sel = window.getSelection(),
range;
range = sel.getRangeAt(0);
range.deleteContents();
var child = document.createElement(nodeName);
child.innerHTML = '​';
range.insertNode(child);
}
var div = document.querySelector('div'),
btn = document.querySelector('button');
btn.addEventListener('click', function() {
insertNode('strong');
div.focus();
});
div.focus();
<div contenteditable></div><button><strong>B</strong></button>
|