« Prev 10 11 12 13 14 15 16 17 18 19 20 Next »

How to add an onchange event to a select box via javascript?

Create Date: March 18, 2019 at 04:30 PM         Tag: JAVASCRIPT         Author Name: Sun, Charles

How to add an onchange event to a select box via javascript?

Here's another way of attaching the event based on W3C DOM Level 2 Events Specification:

  transport_select.addEventListener(
     'change',
     function() { toggleSelect(this.id); },
     false
  );
New Comment

Deserialize JSON and Serilize object in C#

Create Date: March 18, 2019 at 04:22 PM         Tag: JAVASCRIPT         Author Name: Sun, Charles

Cannot deserialize the current JSON array (e.g. [1,2,3])

I think the problem you're having is that your JSON is a list of objects when it comes in and it doesnt directly relate to your root class.

var content would look something like this (i assume):

[
  {
    "id": 3636,
    "is_default": true,
    "name": "Unit",
    "quantity": 1,
    "stock": "100000.00",
    "unit_cost": "0"
  },
  {
    "id": 4592,
    "is_default": false,
    "name": "Bundle",
    "quantity": 5,
    "stock": "100000.00",
    "unit_cost": "0"
  }
]

Note: make use of http://jsonviewer.stack.hu/ to format your JSON.

So if you try the following it should work:

  public static List<RootObject> GetItems(string user, string key, Int32 tid, Int32 pid)
    {
        // Customize URL according to geo location parameters
        var url = string.Format(uniqueItemUrl, user, key, tid, pid);

        // Syncronious Consumption
        var syncClient = new WebClient();

        var content = syncClient.DownloadString(url);

        return JsonConvert.DeserializeObject<List<RootObject>>(content);

    }

You will need to then iterate if you don't wish to return a list of RootObject.


I went ahead and tested this in a Console app, worked fine.

enter image description here

json2csharp

 

Convert JSON String To C# Object

using Newtonsoft.Json;
...
var result = JsonConvert.DeserializeObject<T>(json);

Where T is your object type that matches your JSON string.

 

New Comment

Remove html elements using javascript

Create Date: March 18, 2019 at 09:53 AM         Tag: JAVASCRIPT         Author Name: Sun, Charles

Remove values from select list based on condition

Give an id for the select object like this:

    <select id="mySelect" name="val" size="1" >
     <option value="A">Apple</option>
     <option value="C">Cars</option>
     <option value="H">Honda</option>
     <option value="F">Fiat</option>
     <option value="I">Indigo</option>                    
    </select> 

You can do it in pure JavaScript:

  var selectobject=document.getElementById("mySelect");
  for (var i=0; i<selectobject.length; i++){
  if (selectobject.options[i].value == 'A' )
     selectobject.remove(i);
  }
selectobject.parentNode.removeChild(selectobject);  //fully remove select tag

But - as the other answers suggest - it's a lot easier to use jQuery or some other JS library.

Remove element by id

When removing an element with standard JavaScript, you must go to its parent first:

var element = document.getElementById("element-id");
element.parentNode.removeChild(element);

How to delete a created element in JavaScript?

 

function hideTextBox() {
        var name = 'Author' + (ctr - 1);
        var pTarget = document.getElementById('tbhold');
        var cTarget = document.getElementById(name);
       var tr = cTarget.parentNode.parentNode;
            tr.parentNode.removeChild(tr);
            ctr = ctr - 1;
    }   

Here is Demo

How do I remove a key from a JavaScript object? [duplicate]

The delete operator allows you to remove a property from an object.

The following examples all do the same thing.

// Example 1
var key = "Cow";
delete thisIsObject[key]; 

// Example 2
delete thisIsObject["Cow"];

// Example 3
delete thisIsObject.Cow;

If you're interested, read Understanding Delete for an in-depth explanation.

Removing li elements from ul

var elem = document.getElementById('id');
elem.parentNode.removeChild(elem);
New Comment
« Prev 10 11 12 13 14 15 16 17 18 19 20 Next »