Showing posts with label jquery. Show all posts
Showing posts with label jquery. Show all posts

Wednesday, October 30, 2013

Update variable when checking checkbox

I have a script with some variables, & I would like to update one of these variables depending on the checkbox being checked or not. The checkbox is unselected by default. What I have now is this:

var checked = $('#accept').attr('checked');if (checked) {    permission = "Y";} else {    permission = "N";};var track = {    version: 123,    id: 123456789,    order: {        sv1: 'Y',        sv2: 'N',        sv3: 'Name',        sv4: permission,        orderid: '123xyz'    }};console.log(track);

I would like to update the content of the variable “permission” based on a click event of a checkbox:

$('#accept').click(function () {      if (checked) {      permission = "Y";      } else {      permission = "N";      };});

Fiddle here.

But how is that possible?

In order to update your object key (sv4) value:

http://jsfiddle.net/k6S3m/6/

If you already store the permission into your Object track, well, than don’t hesitate to use your object & directly manipulate it & setting a new value to it’s key using =.

var permission = $('#accept').is(':checked') ? "Y" : "N";var track = {    version: 123,    id: 123456789,    order: {        sv1: 'Y',        sv2: 'N',        sv3: 'Name',        sv4: permission,        orderid: '123xyz'    }};console.log( track.order.sv4 );                       // returns: N$('#accept').change(function () {    track.order.sv4 = this.checked ? "Y" : "N";    console.log( track.order.sv4 );                   // returns: Y});

Never forget to define internally your variables by setting var.

The $('#accept').is(':checked') uses the jQuery .is() method that returns a boolean (true/false) which than we set using Ternary Operator ( ? : ) to "Y" / "N"

Monday, October 7, 2013

Setting base path dynamically through jquery/javascript?

Is there a way to dynamically alter the content inside the tag?, throught jquery or javascript?

I am not using php or any server side script. I am using backbone.js & hence i have a .html file.

I have come to a situation where i would need to type in base path.

Like

<base href="www.google.com" />

I have searched around & i havent received anywhere.

Probably this is not possible at all, yet just wanted to check it here in stackoverflow.

Using jQuery. Here is jsFiddle I’ve made http://jsfiddle.net/uQVeN/

HTML:

<base href="www.google.com" /><div id="valueBefore"></div><div id="valueAfter"></div>

JS:

$(document).ready(function(){    $('#valueBefore').text( $('base').attr('href') );    $('base').attr('href','kjhsgdkjahgdjkahgdf');    $('#valueAfter').text( $('base').attr('href') );});

Will produce:

www.google.comkjhsgdkjahgdjkahgdf

Wednesday, October 2, 2013

Add radio input dynamically to jQuery Mobile fieldset controlgroup

I’ve seen variations on this question yet not my specific case on SO, so here goes.

I’m having trouble adding a new radio input dynamically to an existing control group using jQuery Mobile. It will add, yet the style is not correct & it doesn’t appear to interact with the other inputs correctly.

Here’s my fiddle:
http://jsfiddle.net/cjindustries/3mQF6/

Can anyone see what I’m doing wrong? Or is is a case of regenerating the whole fieldset /controlgroup again? 🙁

Thanks! Code below too…

        <div id="testPage" data-role="page">        <div data-role="content">    <fieldset data-role="controlgroup">        <legend>Radio buttons, vertical controlgroup:</legend>            <input type="radio" name="radio-choice-1" id="radio-choice-1" value="choice-1" checked="checked">            <label for="radio-choice-1">Cat</label>            <input type="radio" name="radio-choice-1" id="radio-choice-2" value="choice-2">            <label for="radio-choice-2">Dog</label>            <input type="radio" name="radio-choice-1" id="radio-choice-3" value="choice-3">            <label for="radio-choice-3">Hamster</label>            <input type="radio" name="radio-choice-1" id="radio-choice-4" value="choice-4">            <label for="radio-choice-4">Lizard</label>    </fieldset>        </div>    </div>$('<input type="radio" name="radio-choice-5" id="radio-choice-5"><label for="radio-choice-5">Horse</label>')    .appendTo("fieldset");$("fieldset").trigger('create');

You need to use name="radio-choice-1" as you have used in above. This solves you behavior problem

Behavior problem solved with below change
Your modified code for dynamically adding radio.

 $('<input type="radio" name="radio-choice-1" id="radio-choice-5"><label for="radio-choice-5">Horse</label>').appendTo("fieldset");

Style problem solved with below change
Just recreate div instead of fieldset

 $("div").trigger('create');

Check Fiddle Demo