I have a form and i must check element in textarea/ div with trumbowyg.
$("#test").keyup(function() {
var val = $(this).val();
if (val.match(/{event_title}/g)) {
$("p.eventTagTitle").addClass("true");
} else {
$("p.eventTagTitle").removeClass("true");
}
if (val.match(/{event_form}/g)) {
$("p.eventTagForm").addClass("true");
} else {
$("p.eventTagForm").removeClass("true");
}
if (val.match(/{event_author}/g)) {
$("p.eventTagAuthor").addClass("true");
} else {
$("p.eventTagAuthor").removeClass("true");
}
});
jsfiddle.net/f1m33312/3
but
in trumbowyg not works
jsfiddle.net/f1m33312/4
similar solution in trumbowyg not work when add text to div
Trumbowyg editor have special event: tbwchange (from http://alex-d.github.io/Trumbowyg/documentation.html).
You should just replace one line
$("#test").keyup(function() {
to
$("#test").on('tbwchange', function() {
Done on fiddle: http://jsfiddle.net/f1m33312/6/
Related
I have two group of checkbox newBuilding & oldBuilding.
Idea over here is I can select checkbox only one of the group.
In each group there is checkbox name other area, so I when click on it, it will show and hide textbox next to it.
Now to achieve first point, lets for example that already we have oldBuilding checkboxes are checked and I if I click one of the newBuilding checkbox then it will remove the check from oldBuilding group but newBuilding checkbox will not get checked but just get focus, I have to click again to check.
What I found out that above issue happen when I call trigger event. How can I overcome the issue
Code for other area
$("#chkOldBuildingOtherAreas").change(function () {
if ($("#chkOldBuildingOtherAreas").is(":checked"))
$("#txOldOtherAreas").show();
else
$("#txOldOtherAreas").hide();
});
$("#chkNewBuildingOtherAreas").change(function () {
if ($("#chkNewBuildingOtherAreas").is(":checked"))
$("#txNewOtherAreas").show();
else
$("#txNewOtherAreas").hide();
});
Code for removing check mark from other groups
$("input[name='oldBuilding']").change(function () {
if ($("input[name='newBuilding']:checked").length > 0) {
$("input[name='newBuilding']").removeAttr('checked');
$("#chkNewBuildingOtherAreas").trigger("change");
}
});
$("input[name='newBuilding']").change(function () {
if ($("input[name='oldBuilding']:checked").length > 0) {
$("input[name='oldBuilding']").removeAttr('checked');
$("#chkOldBuildingOtherAreas").trigger("change");
}
});
My jsfiddle
https://jsfiddle.net/milindsaraswala/wchrwjnx/
https://jsfiddle.net/1ny36nwL/4/
var groups = ['.oldGroup', '.newGroup'];
$(groups.join(',')).find('input[type=text]').hide();
function resetGroup(selector) {
//clear and hide texts
$('input[type=text]', selector).val('').hide();
//uncheck boxes
$('input[type=checkbox]', selector).removeAttr('checked');
}
$("input[name='oldBuilding']").change(function(e) {
if (this.id == 'chkOldBuildingOtherAreas') {
$("#txOldOtherAreas").toggle();
}
resetGroup('.newGroup');
});
$("input[name='newBuilding']").change(function(e) {
if (this.id == 'chkNewBuildingOtherAreas') {
$("#txNewOtherAreas").toggle();
}
resetGroup('.oldGroup');
});
as you can see I added groups var which can contain multiple groups (not only two), but code need to be changed a little more for that to work
you need to detect id/class of current group by something like $(this).closest('.form-group').id and reset every group except current group. in that way you can leave only one change function which will be universal
oh and you also need to add some class for checkbox that contain text input, and if that checkbox is clicked, trigger toggle for input. so it won't be if (this.id == 'chkNewBuildingOtherAreas') { but something like if ($(this).hasClass('has-input'))
Try replacing this in your code. It should work.
$("#txOldOtherAreas").hide();
$("#txNewOtherAreas").hide();
$("input[name='oldBuilding']").change(function (e) {
$("input[name='newBuilding']").removeAttr('checked');
e.target.checked = true;
if (e.target.id == "chkOldBuildingOtherAreas") {
$("#txOldOtherAreas").show();
$("#txNewOtherAreas").hide();
} else {
$("#txNewOtherAreas").hide();
}
});
$("input[name='newBuilding']").change(function (e) {
$("input[name='oldBuilding']").removeAttr('checked');
e.target.checked = true;
if (e.target.id == "chkNewBuildingOtherAreas") {
$("#txNewOtherAreas").show();
$("#txOldOtherAreas").hide();
} else {
$("#txOldOtherAreas").hide();
}
});
You can try following code to fix the problem (Tested in fiddle):
$('#txNewOtherAreas, #txOldOtherAreas').hide();
$('input[name="oldBuilding"]').on('click', function(){
if($('input[name="newBuilding"]').is(':checked')){
$('input[name="newBuilding"]').removeAttr('checked');
$('#txNewOtherAreas').hide();
}
});
$('input[name="newBuilding"]').on('click', function(){
if($('input[name="oldBuilding"]').is(':checked')){
$('input[name="oldBuilding"]').removeAttr('checked');
$('#txOldOtherAreas').hide();
}
});
$('#chkNewBuildingOtherAreas').on('click', function() {
if($(this).is(':checked')){
$('#txNewOtherAreas').show();
} else {
$('#txNewOtherAreas').hide();
}
});
$('#chkOldBuildingOtherAreas').on('click', function() {
if($(this).is(':checked')){
$('#txOldOtherAreas').show();
} else {
$('#txOldOtherAreas').hide();
}
});
if (!$(this).is(":checked")) {
$.each(function(){
if ($('#result').has("Apartment")) {
$('#result').remove();
}
})
}
I want that if I uncheck a checkbox then the div having id="result" should be deleted which contains the text "Apartment". Currently this logic is not working for me.
Try this,
$('#checkboxId').change(function(){
if(!$(this).is(':checked')) {
$('#result:contains(Apartment)').remove();
}
});
DEMO
or
$('#checkboxId').change(function(){
if(!$(this).is(':checked')) {
if($('#result').text().indexOf("mystring")>-1) {
$('#result').remove();
}
}
});
particular id containing particular text?
if( $('elementselector').attr('id').indexOf('Apartment')>-1) { ..... }
unless there is some new JQ method out there
( note - elements must have an ID, or check has ID first etc .. )
Try this http://jsfiddle.net/Tushar490/bg82uw8s/
var IsCheck;
var txt = $("#result").text();
$("#chk").change(function () {
IsCheck = $("#chk").is(":checked");
if (!IsCheck) {
if (txt === 'Apartment') {
$("#result").remove();
}
}
});
I am trying to click a button and toggle series on and off. I do not understand why, but my button's HTML is showing a function and and not its attributes:
$button.click(function() {
alert($('#button').text);
if ($button.text == 'Show previous years')
{
for (var i=0;i<chart.series.length;i++)
{
if (chart.series[i].name.indexOf("13/14")== false)
{
chart.series[i].show();
}
alert($button.html);
$button.html('Hide previous years');
}
} else {
for (var i=0;i<chart.series.length;i++)
{
if (chart.series[i].name.indexOf("13/14")>0)
{
chart.series[i].show();
}
$button.html('Show previous years');
}
}
});
Please take a look: http://jsfiddle.net/pzh20/6cQ2N/8/
You're using .text which is a function.
Replace .text by .text() and it will be the text()'s return value, which is the string you're looking for.
http://jsfiddle.net/6cQ2N/9/
In this edited fiddle, you'll get a good alert value since I changed the .text to .text()
I am trying to add an else statement to this piece of javascript so that if a div-1 is clicked once it shows div-2 and if div-1 is clicked again it hides div-2.
Does anyone know how I could do this?
$(function() {
$('.div-1').click(function() {
$('.div-2').show();
return false;
});
});
Try toggle() instead:
$('.div-1').click(function() {
$('.div-2').toggle();
return false;
});
You can add a dummy class and check for it's exixtence.
$(function() {
$('.div-1').click(function() {
var $this = $(this),
$div2 = $('.div-2');
if($this.hasClass('open')) {
$this.removeClass(open');
$div2.hide();
} else {
$this.addClass(open');
$div2.show();
}
return false;
});
});
I totally agree with Jason P's answer that you should be using toggle instead, but in case you actually needed to use an if statement, you can do this:
$('.div-1').click(function() {
if ($('.div-2').is(':visible')) {
$('.div-2').hide();
} else {
$('.div-2').show();
}
return false;
});
jsFiddle
Okay,
Another novice type question.
I have this array within the head section on my website and want to use it inline JavaScript:
var MyVariable = {"chkboxid":"chkbox"}
chkboxid is a id of a checkbox input.
Now, while validating the checkbox input on form submit, neither this works
$("form#myform").submit(function () {
if ($(MyVariable.chkboxid).is(":checked")) {
} else {
alert(CommentsPlus.nochkboxmsg);
return false;
}
Nor This (check the double quote at the variable)
$("form#myform").submit(function () {
if ($("MyVariable.chkboxid").is(":checked")) {
} else {
alert(CommentsPlus.nochkboxmsg);
return false;
}
However, if I hardcode the checkbox input id, it works. I mean "input#chkbox" in place of MyVariable.chkboxid.
$("form#myform").submit(function () {
if ($("input#chkbox").is(":checked")) {
} else {
alert(CommentsPlus.nochkboxmsg);
return false;
}
How can I use that variable instead of hard coding the input id?
You are missing the "#" before the ID:
$("form#myform").submit(function () {
var element = $("#" + MyVariable.chkboxid)
if (element.is(":checked")) {
} else {
alert(CommentsPlus.nochkboxmsg);
return false;
}
}
Note however that chkboxid is the key, the actual ID of the checkbox should be the value:
var MyVariable = {"chkboxid": "real_id_here"}
please check below code.hope it will work
$("form#myform").submit(function () {
if ($("#chkboxid").is("checked")) {alert("checked");
} else {
alert("not checked");
}
});
thanks,Ripa Saha